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=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 Overflow
Top answer
1 of 3
2

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=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;
};
2 of 3
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
Discussions

How to zip multiple files into individual .gz files [windows]
Are the files all in the same folder? More on reddit.com
🌐 r/techsupport
4
1
April 26, 2021
windows - Gzip compressing multiple files with subfolders into each name using batch and 7zip - Stack Overflow
I have a folder which contains subfolders with multiple html files in it. I want to compress all html files by its own to gzip, step by step, in every subfolder with a 7zip batch (.bat). I dont nee... More on stackoverflow.com
🌐 stackoverflow.com
September 21, 2018
Windows CMD file/folder Compression using 7z or alternative .tar.gz - Stack Overflow
I've searched how to compress files/folders using 7z on windows cmd, I've found the way to compress to: zip, tar, 7z, ..etc but not .tar.gz or .tar.bz2 Here it is the documentation I user: http://... More on stackoverflow.com
🌐 stackoverflow.com
zip - Why can't we gzip more than 1 file using 7zip - Stack Overflow
Recently I noticed that if I try to compress more than 1 file using 7zip, gzip format is not present in the Archive Format-List. Can anyone explain why? Can't we have more than 1 file in gzip? More on stackoverflow.com
🌐 stackoverflow.com
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › how-to-convert-files-to-gzip-on-windows
How to Compress Files to ".gz" on the Windows Operating System
March 1, 2024 - If you simply want to compress any single file to .gzip format, you need to simply select that file and add it to the 7zip archive. In the GUI, you can select the Archive format as "gzip" and that's it!
🌐
Remosoftware
remosoftware.com › info › data storage › what is 7 zip file | how to create multiple compressed folders using 7 zip at once?
What is 7 Zip File | How to Create Multiple Compressed Folders using 7 Zip at Once?
January 24, 2025 - Apart from that, you can archive and unarchive multiple files using 7Zip. Additional Information: If you are curious, is the deleted zip file recovery possible? Then, you can always rely on specialized tools like Remo File Recovery Software to recover deleted ZIP files with ease. ... Winzip can create archives only in .zip format. Conversely, 7Zip can create compressed files in 7z, bzip2, gzip, tar, zip, etc.
Top answer
1 of 3
4

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
2 of 3
3

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

🌐
Linhost
linhost.info › 2012 › 08 › gzip-files-in-windows
How to Gzip Files on Windows - Linhost.info
August 15, 2012 - 7-Zip can be used to create more than 7z compressed files, it can also create GZip compressed files and directories. GZip can compressed a single file
🌐
RootUsers
rootusers.com › home › 11 simple gzip examples
11 Simple Gzip Examples
July 18, 2018 - These gzip examples will show you how to easily compress and decompress data with gzip in 11 useful ways.
🌐
Super User
superuser.com › questions › 1769900 › how-to-unzip-a-multitude-of-gz-files-with-windows-command-prompt
How to unzip a multitude of .gz files with windows command prompt - Super User
February 21, 2023 - Can this be done in a simpler way, like find "timestamp" in all .log* files and IF there's a hit, write it in a file? ... If you are going to go down this path, I suggest installing WSL so the commands aren't foreign or ports. At that point, the problem becomes nothing to do with windows. ... thank you, but sadly i must do this on my workplace computer, so I can't install anything. There were a lot of hits about the tar command, some even said that it's especially for zip and gz but it doesn't work. ... I would try 7zip then.