This sample demonstrates how easy it is to zip a stream with ZipForge.NET
Download ZipForge.NET | Learn More | All C# samples
using System;
// For FileStream class and FileMode enumeration
using System.IO;
// This namespace contains the main class - ZipForge.
// Don't forget to add a reference to the ZipForge
// assembly to your project references
using ComponentAce.Compression.ZipForge;
// This namespace contains ArchiverException class
// required for error handling
using ComponentAce.Compression.Archiver;
namespace ZipStream
{
class Program
{
static void Main(string[] args)
{
// Create an instance of the ZipForge class
ZipForge archiver = new ZipForge();
try
{
// Set the name of the archive file we want to create
archiver.FileName = @"C:\test.zip";
// Because we create a new archive,
// we set fileMode to System.IO.FileMode.Create
archiver.OpenArchive(FileMode.Create);
// Set base (default) directory for all archive operations
archiver.BaseDir = @"C:\";
// Create the new FileStream object for reading from the file.
// If file doesn't exist, a FileNotFoundException will be thrown
FileStream fileStream = new FileStream(@"C:\file.txt",
FileMode.Open);
// Add the stream to the archive.
// Specify the name of the file in the archive,
// where stream data is saved
archiver.AddFromStream("anothername.txt", fileStream);
archiver.CloseArchive();
}
// Catch all exceptions of the ArchiverException type
catch (ArchiverException ae)
{
Console.WriteLine("Message: {0}\t Error code: {1}",
ae.Message, ae.ErrorCode);
// Wait for the key to be pressed
Console.ReadLine();
}
// Additional catch block because the creation
// of the FileStream object can throw exceptions
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
// Wait for the key to be pressed
Console.ReadLine();
}
}
}
}
Download ZipForge.NET | Learn More | All C# samples |