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
afor adding a file to an archive. -tgzipto specify Gzip format."$newFileName.gz"as the archive name.$newFileNameas 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=
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;
};
Answer from Zarepheth on Stack OverflowThis 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
afor adding a file to an archive. -tgzipto specify Gzip format."$newFileName.gz"as the archive name.$newFileNameas 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=
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;
};
If you're not forced to use 7-zip, there are command-line alternatives.
One is to download the gzip.exe binary from https://gnuwin32.sourceforge.net/packages/gzip.htm and run that on the command line:
c:\my_data> gzip.exe -v *.data
I do not guarantee that this binary is safe, but I have no particular reason to believe it is not. Your IT department may not approve of downloading and executing binaries.
Or, if you happen to have either Git for Windows, MinGW compiler or MSYS2 compiler environment installed, those environments include gzip. Open the environment terminal (f.ex. Git Bash):
~ > cd /c/my_data
/C/my_data > gzip -v *.data
Just use the gzip alone.
by default, gzip will take any file passed to it and compress it and add the .gz extension. i.e.
gzip dir/* -r
would gzip every file in dir/* (and sub-directories).
Try the following in powershell (after going to the correct directory):
$files = get-childitem
foreach ($file in $files) {gzip $file}
That will go through all the files in the directory, and compress all of them.
Edit: If you want to do all of the files in a directory tree (i.e. in a folder and all its subfolders) just change $files = get-childitem to $files = get-childitem -recurse
How to zip multiple files into individual .gz files [windows]
windows - Gzip compressing multiple files with subfolders into each name using batch and 7zip - Stack Overflow
Windows CMD file/folder Compression using 7z or alternative .tar.gz - Stack Overflow
zip - Why can't we gzip more than 1 file using 7zip - Stack Overflow
Hi There!
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 for any advice!
7zip will handle gzip format. It also offers lzma compression which is much better than gzip.
If you want a command-line gzipper just like gzip in linux, try this
For those looking to compress multiple files into a .tar.gz file on Windows:
Since 2017, the tar (bsdtar) utility is available on Windows, both in PowerShell and in the basic Command Prompt (cmd).
For a summary of options, run tar --help, for a detailed list of options, visit the docs.
For example, to add the contents of my_directory to my.tar.gz you can do:
tar -czf my.tar.gz --directory my_directory *
where -czf combines the options for "create", "gzip", and "filename", respectively. --directory (or -C) changes directory before adding all content using * (or ., which also works in bash).
To inspect the file's contents:
tar -tf my.tar.gz
where -tf is "list", "filename"
And to extract the content into some_directory:
tar -xf my.tar.gz --directory=some_directory
where -xf is "extract", "filename"
For those that have git for windows installed: git bash includes both GNU tar (as opposed to bsdtar) and the gzip program.
Solved. You can accomplish this by writing a CMD batch file with the following contents:
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -ttar "%%X.tar" "%%X\"
for %%X in (*.tar) do "c:\Program Files\7-Zip\7z.exe" a -tgzip "%%X.gz" "%%X"
Note that the double % signs should be removed if you're trying to directly execute this within an interactive CMD console.
With 7-Zip 9.20 you can avoid the for loop just adding \ at name ends, this works for me:
set "_dir=%1"
set "_7zexe=c:\<my-7z-dir>\7z.exe"
"%_7zexe%" a -ttar %_dir%.tar %_dir%\
"%_7zexe%" a -tgzip %_dir%.gz %_dir%.tar
You can use the x switch and the -o switch with a simple FOR loop using 7zip to complete this task using substitutions accordingly.
The x switch switch tells 7zip to extract files with the full paths. The -o switch specifies the full path to the output directory. The FOR loop %%~NA tells it to name the extracted folder to the same name of the original gz file minus the .gz file extension.
Batch Script Example
@ECHO ON
SET SourceDir=C:\SourceFolder
SET OutputDir=C:\OutputFolder
FOR %%A IN ("%SourceDir%\*.gz") DO 7z x "%%~A" -o"%OutPutDir%\%%~NA"
::::FOR %A IN ("%SourceDir%\*.gz") DO 7z x "%~A" -o"%OutPutDir%\%~NA"
GOTO EOF
Further Resources
- FOR
FOR /?
In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (") %~fI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file %~$PATH:I - searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string
7z --help
Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [<@listfiles...>] <Commands> a : Add files to archive b : Benchmark d : Delete files from archive e : Extract files from archive (without using directory names) h : Calculate hash values for files i : Show information about supported formats l : List contents of archive rn : Rename files in archive t : Test integrity of archive u : Update files to archive x : eXtract files with full paths <Switches> -- : Stop switches parsing -ai[r[-|0]]{@listfile|!wildcard} : Include archives -ax[r[-|0]]{@listfile|!wildcard} : eXclude archives -ao{a|s|t|u} : set Overwrite mode -an : disable archive_name field -bb[0-3] : set output log level -bd : disable progress indicator -bs{o|e|p}{0|1|2} : set output stream for output/error/progress line -bt : show execution time statistics -i[r[-|0]]{@listfile|!wildcard} : Include filenames -m{Parameters} : set compression Method -mmt[N] : set number of CPU threads -o{Directory} : set Output directory -p{Password} : set Password -r[-|0] : Recurse subdirectories -sa{a|e|s} : set Archive name mode -scc{UTF-8|WIN|DOS} : set charset for for console input/output -scs{UTF-8|UTF-16LE|UTF-16BE|WIN|DOS|{id}} : set charset for list files -scrc[CRC32|CRC64|SHA1|SHA256|*] : set hash function for x, e, h commands -sdel : delete files after compression -seml[.] : send archive by email -sfx[{name}] : Create SFX archive -si[{name}] : read data from stdin -slp : set Large Pages mode -slt : show technical information for l (List) command -snh : store hard links as links -snl : store symbolic links as links -sni : store NT security information -sns[-] : store NTFS alternate streams -so : write data to stdout -spd : disable wildcard matching for file names -spe : eliminate duplication of root folder for extract command -spf : use fully qualified file paths -ssc[-] : set sensitive case mode -ssw : compress shared files -stl : set archive timestamp from the most recently modified file -stm{HexMask} : set CPU thread affinity mask (hexadecimal number) -stx{Type} : exclude archive type -t{Type} : Set type of archive -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName] : Update options -v{Size}[b|k|m|g] : Create volumes -w[{path}] : assign Work directory. Empty path means a temporary directory -x[r[-|0]]{@listfile|!wildcard} : eXclude filenames -y : assume Yes on all queries
This is the full recursive solution.
@ECHO ON
SET SourceDir=C:\source
FOR /R %SourceDir% %%A IN ("*.gz") DO 7z x "%%~A" -o"%%~pA\"
This doesn't delete original .gz files, I think it can be done with some 7z parameter or simply adding a delete %%~A command in the FOR loop
if you have zip,
zip myzip.zip cvd*.txt
Don't need to tar them first.
You want to tar your files together and gzip the resulting tar file.
tar cvzf cvd.tar.gz cvd*.txt
To untar the gzip'd tar file you would do:
tar xvzf cvd.tar.gz -C /path/to/parent/dir
This would extract your files under the /path/to/parent/dir directory
Hi all,
I need to compress thousands of files into thousands of individual files, like:
PC1.txt > PC1.7z/ zip
PC2.txt > PC2.7z/ zip
PC3.txt > PC3.7z/ zip
I've read the command line for 7zip but this is absolutely not working on my end (Windows 11 PC)
Is there any way to get this to work? I would also change my program to do this as long as it is near the compression level of 7zip.
