简体   繁体   中英

Zip multiple files into individual .gz files (windows)

Hoping somebody might be able to help me find a quicker way to do this:

I have 640 files that each need to be individually zipped into their own.gz file.

7-zip only allows zipping of 1 file to.gz at a time. Is there a way I can do all 640 in one go, while retaining them as individual files? (running windows)

Thanks in advance!

This answer is a bit late, but to help myself and others who come across this in the future, one can use a shell script to execute the 7Zip command line on each file.

The key lines below are the foreach loop and the PowerShell call operator for the 7z.exe executable.

  • Command a for adding a file to an archive.
  • -tgzip to specify Gzip format.
  • "$newFileName.gz" as the archive name.
  • $newFileName as the name of the file to be zipped.

Warning! In my use case, I was renaming files with new dates in the filenames, you might want to change the logic...

PowerShell Example:

$files=[System.IO.FileInfo[]](Get-ChildItem ./*.txt);

$now=[System.DateTime]::Now.Date;
$span=[System.TimeSpan]::FromDays($files.Length);
$startDate=$now - $span;
$files=Sort-Object -InputObject $files -Property Name;

$index=0;
foreach ($file in $files) {
    $newDateText = ($startDate + [TimeSpan]::FromDays($index)).ToString('yyyyMMdd');
    $newFileName = "myPrefix.$newDateText.txt";
    Write-Host "$($file.Name) => " -ForegroundColor Cyan -NoNewline;
    Write-Host "$newFileName.gz" -ForegroundColor Green;
    #Rename-Item -Path $file -NewName $newFileName;
    & "c:\Program Files\7-Zip\7z.exe" a -tgzip "$newFileName.gz" $newFileName
    $index += 1;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM