Skip to content
Matthias Fussenegger edited this page Apr 5, 2018 · 7 revisions

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.Key);
			reader.WriteEntryToDirectory(@"C:\temp", new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
		}
	}
}

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.Key);
		entry.WriteToDirectory(@"C:\temp", new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
	}
}

ZipArchive with Writing API Example:

using (var archive = ZipArchive.Create())
{
	archive.AddAllFromDirectory(@"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(zip, ArchiveType.Zip, CompressionType.Deflate))
{
     foreach (var filePath in filesList)
     {
        zipWriter.Write(Path.GetFileName(file), filePath);
     }
}

Creating tar.bz2 with Writer API

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