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:
- Load a ZIP archive
- Get the root metadata package
- Extract the native metadata package using ZipRootPackage.ZipPackage
- Read the ZIP archive properties
- Loop through ZipPackage.Files to extract information about archived files
The following code snippet shows how to get metadata from a ZIP archive.
AdvancedUsage.ManagingMetadataForSpecificFormats.
Encoding encoding = Encoding.GetEncoding(866);
using (Metadata metadata = new Metadata(Constants.InputZip))
{
var root = metadata.GetRootPackage<ZipRootPackage>();
Console.WriteLine(root.ZipPackage.Comment);
Console.WriteLine(root.ZipPackage.TotalEntries);
foreach (var file in root.ZipPackage.Files)
{
Console.WriteLine(file.Name);
Console.WriteLine(file.CompressedSize);
Console.WriteLine(file.CompressionMethod);
Console.WriteLine(file.Flags);
Console.WriteLine(file.ModificationDateTime);
Console.WriteLine(file.UncompressedSize);
// Use a specific encoding for the file names
Console.WriteLine(encoding.GetString(file.RawName));
}
}
Updating the user comment
GroupDocs.Metadata for .NET allows you to update the user comment in a ZIP archive. The following steps are needed to be followed:
- Load a ZIP archive
- Get the root metadata package
- Update the comment using the ZipPackage.Comment setter
- Save the changes
The following code snippet demonstrates the usage of this feature.
AdvancedUsage.ManagingMetadataForSpecificFormats.
using (Metadata metadata = new Metadata(Constants.InputZip))
{
var root = metadata.GetRootPackage<ZipRootPackage>();
root.ZipPackage.Comment = "updated comment";
metadata.Save(Constants.OutputZip);
}
Removing the user comment
GroupDocs.Metadata for .NET allows you to remove the user comment associated with a ZIP archive. The following code snippet demonstrates the usage of this feature.
AdvancedUsage.ManagingMetadataForSpecificFormats.
using (Metadata metadata = new Metadata(Constants.InputZip))
{
var root = metadata.GetRootPackage<ZipRootPackage>();
root.ZipPackage.Comment = 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 .NET 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.