Working with ZIP archives

Get ZIP format metadata

The API allows detecting ZIP archives and reading format metadata. The following steps are needed to be followed:

The following code snippet shows how to get metadata from a ZIP archive.

advanced_usage.managing_metadata_for_specific_formats.archive.ZipReadNativeMetadataProperties

Charset charset = Charset.forName("cp866");

try (Metadata metadata = new Metadata(Constants.InputZip)) {
    ZipRootPackage root = metadata.getRootPackageGeneric();

    System.out.println(root.getZipPackage().getComment());
    System.out.println(root.getZipPackage().getTotalEntries());

    for (ZipFile file : root.getZipPackage().getFiles()) {
        System.out.println(file.getName());
        System.out.println(file.getCompressedSize());
        System.out.println(file.getCompressionMethod());
        System.out.println(file.getFlags());
        System.out.println(file.getModificationDateTime());
        System.out.println(file.getUncompressedSize());

        // Use a specific encoding for the file names
        System.out.println(new String(file.getRawName(), charset));
    }
}

Updating the user comment

GroupDocs.Metadata for Java allows you to update the user comment in a ZIP archive. The following steps are needed to be followed:

The following code snippet demonstrates the usage of this feature.

advanced_usage.managing_metadata_for_specific_formats.archive.ZipUpdateArchiveComment

try (Metadata metadata = new Metadata(Constants.InputZip)) {
	ZipRootPackage root = metadata.getRootPackageGeneric();

	root.getZipPackage().setComment("updated comment");

	metadata.save(Constants.OutputZip);
}

Removing the user comment

GroupDocs.Metadata for Java allows you to remove the user comment associated with a ZIP archive. The following code snippet demonstrates the usage of this feature.

advanced_usage.managing_metadata_for_specific_formats.archive.ZipRemoveArchiveComment

try (Metadata metadata = new Metadata(Constants.InputZip)) {
	ZipRootPackage root = metadata.getRootPackageGeneric();

	root.getZipPackage().setComment(null);

	metadata.save(Constants.OutputZip);
}

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.