Working with RAR, 7z, TAR and compressed archives

Besides ZIP archives, GroupDocs.Metadata for Java reads format metadata of the following archive types:

FormatExtensionsRoot packageWhat you can read
RAR.rar, .cbrRarRootPackageTotal entries; name, sizes and modification date of each file
7z.7z, .cb7SevenZipRootPackageTotal entries; name, sizes and modification date of each file
TAR.tar, .cbtTarRootPackageTotal entries; name and size of each file
Apple Archive.aarAarRootPackageTotal entries, solid flag; name, length and directory flag of each entry
GZIP.gz, .gzipGzipRootPackageName, total entries, compressed and uncompressed size
BZ2.bz2Bz2RootPackageName, total entries, size
XZ.xzXzRootPackageName, total entries, size
Zstandard.zst, .zstdZstdRootPackageName, total entries, size

The properties of these packages are read-only. To find out what is supported for a particular format, see Get supported file formats.

Reading RAR archive metadata

  1. Load a RAR archive
  2. Get the root metadata package
  3. Read the total number of entries with RarPackage.getTotalEntries
  4. Loop through RarPackage.getFiles to extract information about the archived files

advanced_usage.managing_metadata_for_specific_formats.archive.RarReadNativeMetadataProperties

try (Metadata metadata = new Metadata(Constants.InputRar)) {
    RarRootPackage root = metadata.getRootPackageGeneric();
    System.out.println(root.getRarPackage().getTotalEntries());

    for (RarFile file : root.getRarPackage().getFiles()) {
        System.out.println(file.getName());
        System.out.println(file.getCompressedSize());
        System.out.println(file.getModificationDateTime());
        System.out.println(file.getUncompressedSize());
    }
}

Reading 7z archive metadata

advanced_usage.managing_metadata_for_specific_formats.archive.SevenZipReadNativeMetadataProperties

try (Metadata metadata = new Metadata(Constants.InputSevenZip)) {
    SevenZipRootPackage root = metadata.getRootPackageGeneric();
    System.out.println(root.getSevenZipPackage().getTotalEntries());

    for (SevenZipFile file : root.getSevenZipPackage().getFiles()) {
        System.out.println(file.getName());
        System.out.println(file.getCompressedSize());
        System.out.println(file.getModificationDateTime());
        System.out.println(file.getUncompressedSize());
    }
}

Reading TAR archive metadata

advanced_usage.managing_metadata_for_specific_formats.archive.TarReadNativeMetadataProperties

try (Metadata metadata = new Metadata(Constants.InputTar)) {
    TarRootPackage root = metadata.getRootPackageGeneric();
    System.out.println(root.getTarPackage().getTotalEntries());

    for (TarFile file : root.getTarPackage().getFiles()) {
        System.out.println(file.getName());
        System.out.println(file.getSize());
    }
}

Reading Apple Archive (AAR) metadata

An Apple Archive can be solid, that is compressed as a single stream. The AarPackage.isSolid method tells you which kind of archive you have.

advanced_usage.managing_metadata_for_specific_formats.archive.AarReadNativeMetadataProperties

try (Metadata metadata = new Metadata(Constants.InputAar)) {
    AarRootPackage root = metadata.getRootPackageGeneric();
    AarPackage archive = root.getAarPackage();

    System.out.println(archive.getTotalEntries());
    System.out.println(archive.isSolid());

    for (AarFile file : archive.getFiles()) {
        System.out.println(file.getName());
        System.out.println(file.getLength());
        System.out.println(file.isDirectory());
    }
}

Reading GZIP archive metadata

A GZIP file compresses a single file. Its package contains the name of the compressed file and both the compressed and the uncompressed size.

advanced_usage.managing_metadata_for_specific_formats.archive.GzipReadNativeMetadataProperties

try (Metadata metadata = new Metadata(Constants.InputGzip)) {
    GzipRootPackage root = metadata.getRootPackageGeneric();
    GzipPackage archive = root.getGzipPackage();

    System.out.println(archive.getTotalEntries());
    System.out.println(archive.getName());
    System.out.println(archive.getUncompressedSize());
    System.out.println(archive.getSize());
}

Reading BZ2, XZ and Zstandard archive metadata

BZ2, XZ and Zstandard packages have the same set of properties: the name of the compressed file, the total number of entries and the size. The code below reads a BZ2 archive.

advanced_usage.managing_metadata_for_specific_formats.archive.Bz2ReadNativeMetadataProperties

try (Metadata metadata = new Metadata(Constants.InputBz2)) {
    Bz2RootPackage root = metadata.getRootPackageGeneric();
    Bz2Package archive = root.getBz2Package();

    System.out.println(archive.getTotalEntries());
    System.out.println(archive.getName());
    System.out.println(archive.getSize());
}

For the other two formats, use the matching root package:

FormatRoot packageGetterExample
XZXzRootPackagegetXzPackage()archive.XzReadNativeMetadataProperties
ZstandardZstdRootPackagegetZstdPackage()archive.ZstdReadNativeMetadataProperties
try (Metadata metadata = new Metadata(Constants.InputZstd)) {
    ZstdRootPackage root = metadata.getRootPackageGeneric();
    ZstdPackage archive = root.getZstdPackage();

    System.out.println(archive.getTotalEntries());
    System.out.println(archive.getName());
    System.out.println(archive.getSize());
}

Loading archives from a stream

The formats on this page are recognized by the file extension. When an archive is loaded from an InputStream or a URI, there is no file name, so set the format explicitly with LoadOptions. Without it, the archive is not recognized correctly: a RAR archive, for example, is detected as an MP3 file.

try (InputStream stream = new FileInputStream(Constants.InputRar);
     Metadata metadata = new Metadata(stream, new LoadOptions(FileFormat.Rar))) {
    RarRootPackage root = metadata.getRootPackageGeneric();
    System.out.println(root.getRarPackage().getTotalEntries());
}

The matching FileFormat values are Rar, SevenZip, Tar, Aar, Gzip, Bz2, Xz and Zstd.

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.