删除文件中的前n行(带有许多“奇怪”符号的.zip文件)

时间:2018-02-28 05:09:18

标签: powershell batch-file zip edit

此powershell代码删除文件中的前4行

from google.cloud import storage
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('bucket-id-here')
# Then do other things...
blob = bucket.get_blob('remote/path/to/file.txt')
print(blob.download_as_string())
blob.upload_from_string('New contents!')
blob2 = bucket.blob('remote/path/storage.txt')
blob2.upload_from_filename(filename='/local/path.txt')

(gc "old.zip" | select -Skip 4) | sc "new.zip" 文件有 Unix(LF)行结尾

此代码还将文件行结尾转换为 Windows(CR LF)

  

如何删除前4行而不进行转换?

由于 .zip 中存在许多“奇怪”符号,因此删除.zip文件中前n行的其他方法不起作用。例如,old.zip中的more than +4 "old.zip"> "new.zip"无效,等等。

通过powershell这样的东西被移除但也没有问题。

  

您是否知道删除 .zip 文件中前n行的其他方法?

2 个答案:

答案 0 :(得分:0)

这样的事情?

$PathZipFile="c:\temp\File_1.zip"
$NewPathZipFile="c:\temp\NewFile_1.zip"

#create temporary directory name
$TemporyDir=[System.IO.Path]::Combine("c:\temp", [System.IO.Path]::GetRandomFileName())

#extract archive into temporary directory
Expand-Archive "c:\temp\File_1.zip" -DestinationPath $TemporyDir

#loop file for remove 4 first lines for every files and compress in new archive
Get-ChildItem $TemporyDir -file | %{

    (Get-Content $_.FullName | select -Skip 4) | Set-Content -Path $_.FullName;
    Compress-Archive $_.FullName $NewPathZipFile -Update

}

Remove-Item  $TemporyDir -Recurse -Force

答案 1 :(得分:-1)

的PowerShell:

(gc "old.txt" | select -Skip 4 | Out-String) -replace "`r`n", "`n" | Out-File "new.txt"

C#:

File.WriteAllText("new.txt", string.Join("\n", File.ReadLines("old.txt").Skip(4)));