is there an equivalent of for .7z files?

  • Last Update :
  • Techknowledgy :

I have to frequently search through a couple of .7z (zipped with LZMA) files. I don't have enough memory to have them unpacked at the same time or to change the archive to .gz. At the moment I unpack one, search for what I need, delete what was extracted, unpack the next. I want to go through the archives in the same way as with gzip:

f ='archive.gz')
for i in f:
   do stuff

Suggestion : 2

The gzip module provides the GzipFile class, as well as the open(), compress() and decompress() convenience functions. The GzipFile class reads and writes gzip-format files, automatically compressing or decompressing the data so that it looks like an ordinary file object.,The gzip module provides a simple command line interface to compress or decompress files.,This module provides a simple interface to compress and decompress files just like the GNU programs gzip and gunzip would.,Compress the data, returning a bytes object containing the compressed data. compresslevel and mtime have the same meaning as in the GzipFile constructor above.

import gzip
with'/home/joe/file.txt.gz', 'rb') as f:
   file_content =
import gzip
content = b "Lots of content here"
with'/home/joe/file.txt.gz', 'wb') as f:
import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
   with'/home/joe/file.txt.gz', 'wb') as f_out:
   shutil.copyfileobj(f_in, f_out)
import gzip
s_in = b "Lots of content here"
s_out = gzip.compress(s_in)

Suggestion : 3

import gzip
f_in = open('/home/joe/file.txt')
f_out ='/home/joe/file.txt.gz', 'wb')
import gzip
import shutil
with'file.txt.gz', 'rb') as f_in:
   with open('file.txt', 'wb') as f_out:
   shutil.copyfileobj(f_in, f_out)

Suggestion : 4

7-Zip is an Archive and File Management utility available in command-line versions for Linux/Mac, "P7Zip" (7z.exe), as well as for Windows, "7za" (7za.exe). Although its interface is deceptively simple, the command-line versions of 7ZIP are highly customizable archiving programs when used with the command parameters and switchesdescribed below.  Windows users who want to use the command-line version should generate a Help Desk ticket to install the standalone 7za.exe version.,NOTE TO WINDOWS USERS: the following syntax examples begin by invoking the Linux command-line version, "7z".  Please change the invocation to "7za" when applying these examples for use in 7-Zip for Windows.,To begin a session, open a terminal window. Invoke the version of 7Zip you are using by entering "7z" for P7Zip (7z.exe), or "7za" for 7Zip for Windows (7za.exe) to start either the P7-Zip or 7za application prior to entering commands.  Other than this program invocation command, all commands, parameters and switches are identical for all  command-line versions., File Access & Archiving File Permissions from UNIX File Permissions from Windows Burning CDs and DVDs for Linux 7Zip Accessing Files

Config file Examples

!@Install @!UTF - 8!
   Title = "7-Zip 4.00"
BeginPrompt = "Do you want to install the 7-Zip 4.00?"
RunProgram = "setup.exe";
!@InstallEnd @!
Title="7-Zip 4.00"
BeginPrompt="Do you want to install the 7-Zip 4.00?"
!@Install @!UTF - 8!
   Title = "7-Zip 4.00"
BeginPrompt = "Do you want to install the 7-Zip 4.00?"
ExecuteFile = "7zip.msi";
!@InstallEnd @!
Title="7-Zip 4.00"
BeginPrompt="Do you want to install the 7-Zip 4.00?"
!@Install @!UTF - 8!
   Title = "7-Zip 4.01 Update"
BeginPrompt = "Do you want to install the 7-Zip 4.01 Update?"
ExecuteFile = "msiexec.exe"
ExecuteParameters = "/i 7zip.msi REINSTALL=ALL REINSTALLMODE=vomus";
!@InstallEnd @!

Suggestion : 5

py7zr works well, but slower than 7-zip and p7zip C/C++ implementation by several reasons. When compression/decompression speed is important, it is recommended to use these alternatives through python interface.,py7zr is a library and utility to support 7zip archive compression, decompression, encryption and decryption written by Python programming language.,py7zr uses a python3 standard lzma module for extraction and compression. The standard lzma module uses liblzma that support core compression algorithm of 7zip.,A feature handling symbolic link is basically compatible with p7zip implementation, but not work with original 7-zip because the original does not implement the feature.

You can install py7zr as usual other libraries using pip.

$ pip install py7zr

OR, alternatively using conda:

$ conda install - c conda - forge py7zr
  • List archive contents
$ py7zr l test .7 z
  • Extract archive
$ py7zr x test .7 z
  • Extract archive with password
$ py7zr x - P test .7 z
password ? : ** **
  • Create multi-volume archive
$ py7zr c - v 500 k target .7 z test_dir
  • Test archive
$ py7zr t test .7 z

Here is a code snippet how to decompress some file in your application.

import py7zr

archive = py7zr.SevenZipFile('sample.7z', mode = 'r')
archive.extractall(path = "/tmp")

You can also use ‘with’ block because py7zr provide context manager(v0.6 and later).

import py7zr

with py7zr.SevenZipFile('sample.7z', mode = 'r') as z:

with py7zr.SevenZipFile('target.7z', 'w') as z:

py7zr also supports extraction of single or selected files by ‘extract(targets=[‘file path’])’. Note: if you specify only a file but not a parent directory, it will fail.

import py7zr
import re

filter_pattern = re.compile(r'<your /target/file_and_directories/regex/expression>')
   with SevenZipFile('archive.7z', 'r') as archive:
   allfiles = archive.getnames()
   selective_files = [f for f in allfiles if filter_pattern.match(f)]

Here is a code snippet how to produce archive.

import py7zr

with py7zr.SevenZipFile('target.7z', 'w') as archive:
   archive.writeall('/path/to/base_dir', 'base')

To create encrypted archive, please pass a password.

import py7zr

with py7zr.SevenZipFile('target.7z', 'w', password = 'secret') as archive:
   archive.writeall('/path/to/base_dir', 'base')

To create archive with algorithms such as zstandard, you can call with custom filter.

import py7zr

my_filters = [{
   "id": py7zr.FILTER_ZSTD
another_filters = [{
   "id": py7zr.FILTER_ARM
}, {
   "id": py7zr.FILTER_LZMA2,
   "preset": 7
with py7zr.SevenZipFile('target.7z', 'w', filters = my_filter) as archive:
   archive.writeall('/path/to/base_dir', 'base')

py7zr also support shutil interface.

from py7zr
import pack_7zarchive, unpack_7zarchive
import shutil

# register file format at first.
shutil.register_archive_format('7zip', pack_7zarchive, description = '7zip archive')
shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)

# extraction
shutil.unpack_archive('test.7z', '/tmp')

# compression
shutil.make_archive('target', '7zip', 'src')

Suggestion : 6

Last Published: 31 July 2021

CompressorOutputStream gzippedOut = new CompressorStreamFactory()
   .createCompressorOutputStream(CompressorStreamFactory.GZIP, myOutputStream);
ArchiveInputStream input = new ArchiveStreamFactory()
CompressorInputStream input = new CompressorStreamFactory()
File targetDir = ...
   try (ArchiveInputStream i = ...create the stream
      for your format, use buffering...) {
      ArchiveEntry entry = null;
      while ((entry = i.getNextEntry()) != null) {
         if (!i.canReadEntryData(entry)) {
            // log something?
         String name = fileName(targetDir, entry);
         File f = new File(name);
         if (entry.isDirectory()) {
            if (!f.isDirectory() && !f.mkdirs()) {
               throw new IOException("failed to create directory " + f);
         } else {
            File parent = f.getParentFile();
            if (!parent.isDirectory() && !parent.mkdirs()) {
               throw new IOException("failed to create directory " + parent);
            try (OutputStream o = Files.newOutputStream(f.toPath())) {
               IOUtils.copy(i, o);
try (InputStream fi = Files.newInputStream(Paths.get("my.tar.gz")); InputStream bi = new BufferedInputStream(fi); InputStream gzi = new GzipCompressorInputStream(bi); ArchiveInputStream o = new TarArchiveInputStream(gzi)) {}
Collection<File> filesToArchive = ...
try (ArchiveOutputStream o = ... create the stream for your format ...) {
    for (File f : filesToArchive) {
        // maybe skip directories for formats like AR that don't store directories
        ArchiveEntry entry = o.createArchiveEntry(f, entryName(f));
        // potentially add more flags to entry
        if (f.isFile()) {
            try (InputStream i = Files.newInputStream(f.toPath())) {
                IOUtils.copy(i, o);