public class RealTimeZip : FileSystem
Shows how to use FindNextItemHeader method to find archive item header and ExtractFile method to extract the found item.
using System.IO; using ComponentPro.Compression; ... // Create a new instance of a real-time zip object. RealTimeZip realTimeZip = new RealTimeZip(); // Specify the stream where the being compressed file will be saved. realTimeZip.CompressedStream = new FileStream(@"C:\Test\archive.zip", FileMode.Open); // Open the archive. realTimeZip.Open(false); const string OutputDir = @"C:\Temp"; // Find next archive item header record within the stream. // The archive item record contains information about the file like file name, size, attributes, comment, etc. // The compressed data follows the archive item header. To read and extract the compressed data, // we need to read the header first. ArchiveItem header; while ((header = realTimeZip.FindNextItemHeader()) != null) { string path = System.IO.Path.Combine(OutputDir, header.FullName); string dir = System.IO.Path.GetDirectoryName(path); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // Extract to a file. realTimeZip.ExtractFile(path); // The above code fragment equals to the following commented code: // using (FileStream fileStream = new FileStream(@"C:\Temp\" + header.Name, // FileMode.Create, FileAccess.Write, FileShare.Write)) // { // realTimeZip.ExtractFile(fileStream); // } } // Save the central directory to the archive. realTimeZip.Close(); // We need to close the CompressedStream manually because // the Close method just write the central directory information to the archive. realTimeZip.CompressedStream.Close();
Shows how to use AddFile method to add file to an archive.
using ComponentPro.Compression; using System.IO; ... // Create a new instance of the RealTimeZip class. RealTimeZip realTimeZip = new RealTimeZip(); // Specify the stream where the being compressed file will be saved. realTimeZip.CompressedStream = new FileStream(@"C:\Temp\archive.zip", FileMode.Create); // Open the archive and set creating a new one. realTimeZip.Open(true); // Add a file to the archive. using(FileStream fileStream = new FileStream(@"C:\Test\test1.dat", FileMode.Open)) { realTimeZip.AddFile(fileStream, "file.txt"); } // Add a file to the archive with comment. using (FileStream fileStream2 = new FileStream(@"C:\Test\test2.dat", FileMode.Open)) { realTimeZip.AddFile(fileStream2, "file2.txt", "comment for my file"); } // or with a simple usage to add a file on local disk. realTimeZip.AddFile(@"C:\test\file.dat", "myfile.dat", "my comment"); // Save the central directory to the archive. realTimeZip.Close(); // We need to close the CompressedStream manually because // the Close method just write the central directory information to the archive. realTimeZip.CompressedStream.Close();