Integrating Compression API in a .NET Application
The compression library integration is simple whereas most of the compress & decompress operations can be performed with just a few lines of code.
Single file compression - C#
var dir = "full directory path";
using (var arch = new Archive()){
arch.CreateEntry("filename.dat", dir+"file.dat");
arch.Save(dir+"result.zip");
}
This code creates a ZIP archive named "result.zip" and adds a single file to it. Here’s a breakdown of what it does:
- Directory Path : It defines a variable named dir and assigns a string containing the full directory path where the files reside.
- Creating the Archive : The code utilizes a using construct, ensuring proper resource management by automatically closing the archive object (arch) after the code within the block finishes executing.
- Adding File to Archive : Inside the using block, the code creates an entry in the archive named "filename.dat". The CreateEntry method takes two arguments:
- The first argument specifies the name of the entry within the archive "filename.dat".
- The second argument provides the path to the actual file that will be added ("dir + file.dat"). This path combines the directory path stored in dir with the filename "file.dat". - Saving the Archive : Finally, the code calls the Save method on the archive object (arch). This saves the newly created ZIP archive with the added file to a file named "result.zip" within the same directory specified by dir.
Aspose.ZIP for .NET is a flexible documents compression and archive manipulation API for standard ZIP format. API enables .NET applications to achieve files compression/decompression, archiving files as well as folders and encryption of archives. This API works with all well known archive formats: ZIP, RAR, 7Zip, GZIP, BZ2. And with many archive formats from Linux: CPIO, TAR, Lzip, Bzip2, XZ, Z. It provides protection via user-defined passwords and traditional encryption using ZipCrypto or AES encryption such as AES128, 192 and AES256.
Aspose.Zip introduces a robust .NET API for File Compression and Archiving, meticulously crafted to meet the evolving demands of contemporary data management. This comprehensive library empowers developers to efficiently compress, archive, and extract files within the .NET framework, ensuring seamless integration and unparalleled performance.
In recent years, the .NET framework has solidified its position as a versatile platform for various applications, from web development to enterprise solutions. Aspose.Zip aligns with this trend, offering a sophisticated toolset for handling file compression tasks, contributing to the efficiency and reliability of ZIP NET based software development.
Advanced .NET Compression & Archiving API Features
Create an archive from one or more files
Encrypt or decrypt whole archive or specific entries
Add entries to existing archive
Compress folder respecting its structure
Decompress to directory respecting archive structure
Compress any size documents by applying parallelism
How to Convert Zip Using C#
In .NET, you can use the ZipFile class to work with ZIP files and compression. If encryption is required, you may want to look into libraries or methods that support password protection for ZIP archives. Always ensure proper security practices when handling sensitive data and encryption in your applications.
The Compression API in a ZIP .NET application refers to a set of functionalities that enable developers to efficiently compress and decompress data. It provides a standardized way to implement data compression algorithms, improving storage efficiency and reducing data transfer times. With the Compression API in .NET, developers can choose from various compression algorithms, such as gzip or deflate, and seamlessly integrate them into their applications. This feature is particularly valuable when working with large datasets or when optimizing network communication in web applications, contributing to enhanced performance and resource utilization.
Convert from ZIP to 7Z via .NET
using (SevenZipArchive sevenZipArchive = new SevenZipArchive())
{
using (Archive archive = new Archive("source.zip"))
{
for (int i = 0; i < archive.Entries.Count; i++)
{
var ms = new MemoryStream();
archive.Entries[i].Extract(ms);
ms.Seek(0, SeekOrigin.Begin);
sevenZipArchive.CreateEntry(archive.Entries[i].Name.Replace('\\', '/'), ms);
}
}
sevenZipArchive.Save("output.7z");
}
The code snippet showcases the conversion of a ZIP archive to a 7Z format via C#. Leveraging the SevenZipArchive library, the script extracts the ZIP entries and creates corresponding entries in the 7Z archive.
Other Supported Aspose.ZIP for .NET API Features
Use the Aspose.ZIP C# library to convert, merge, edit zip-file documents, extract data from the arhives, and more!
Support and Learning Resources
- Learning Resources
- Documentation
- Source Code
- API References
- Tutorial Videos
- Product Support
- Free Support
- Paid Support
- Blog
- Release Notes
- Why Aspose.ZIP for .NET?
- Customers List
- Success Stories