Reader (forward-only streams) API Example:

using (Stream stream = File.OpenRead(@"C:\Code\sharpcompress.rar"))
{
	var reader = ReaderFactory.Open(stream);
	while (reader.MoveToNextEntry())
	{
		if (!reader.Entry.IsDirectory)
		{
			Console.WriteLine(reader.Entry.FilePath);
			reader.WriteEntryToDirectory(@"C:\temp", ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
		}
	}
}

Archive (random access) API Example:

var archive = ArchiveFactory.Open(@"C:\Code\sharpcompress\TestArchives\sharpcompress.zip");
foreach (var entry in archive.Entries)
{
	if (!entry.IsDirectory)
	{
		Console.WriteLine(entry.FilePath);
		entry.WriteToDirectory(@"C:\temp", ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
	}
}

ZipArchive with Writing API Example:

using (var archive = ZipArchive.Create())
{
	archive.AddAllFromDirectoryEntry(@"C:\\source");
	archive.SaveTo("@C:\\new.zip");
}

Creating Zip with Writer API

using (var zip = File.OpenWrite("C:\\test.zip"))
using (var zipWriter = WriterFactory.Open(ArchiveType.Zip, zip))
{
     foreach (var filePath in filesList)
     {
        zipWriter.Write(Path.GetFileName(file), filePath);
     }
}

Creating tar.bz2 with Writer API

using (Stream stream = File.OpenWrite(tarPath))
using (var writer = WriterFactory.Open(ArchiveType.Tar, stream))
{
	writer.WriteAll(filesPath, "*", SearchOption.AllDirectories);
}
using (Stream stream = File.OpenWrite(tarbz2Path))
using (var writer = WriterFactory.Open(ArchiveType.BZip2, stream))
{
	writer.Write("Tar.tar", tarPath);
}

Last edited Mar 30, 2012 at 8:03 AM by adamhathcock, version 5