Working with RAR, 7z, TAR and compressed archives
Leave feedback
Besides ZIP archives, GroupDocs.Metadata for Java reads format metadata of the following archive types:
| Format | Extensions | Root package | What you can read |
|---|---|---|---|
| RAR | .rar, .cbr | RarRootPackage | Total entries; name, sizes and modification date of each file |
| 7z | .7z, .cb7 | SevenZipRootPackage | Total entries; name, sizes and modification date of each file |
| TAR | .tar, .cbt | TarRootPackage | Total entries; name and size of each file |
| Apple Archive | .aar | AarRootPackage | Total entries, solid flag; name, length and directory flag of each entry |
| GZIP | .gz, .gzip | GzipRootPackage | Name, total entries, compressed and uncompressed size |
| BZ2 | .bz2 | Bz2RootPackage | Name, total entries, size |
| XZ | .xz | XzRootPackage | Name, total entries, size |
| Zstandard | .zst, .zstd | ZstdRootPackage | Name, 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.
- Load a RAR archive
- Get the root metadata package
- Read the total number of entries with RarPackage.getTotalEntries
- Loop through RarPackage.getFiles to extract information about the archived files
advanced_usage.managing_metadata_for_specific_formats.
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());
}
}
advanced_usage.managing_metadata_for_specific_formats.
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());
}
}
advanced_usage.managing_metadata_for_specific_formats.
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());
}
}
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.
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());
}
}
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.
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());
}
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.
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:
| Format | Root package | Getter | Example |
|---|---|---|---|
| XZ | XzRootPackage | getXzPackage() | archive.XzReadNativeMetadataProperties |
| Zstandard | ZstdRootPackage | getZstdPackage() | 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());
}
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.
You may easily run the code above and see the feature in action in our GitHub examples:
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.