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

Comments

HeikkiSiltala Dec 13, 2012 at 8:23 AM 
The examples seem to be outdated, but I managed to scratch up a bit of code that makes a zip file, but it is corrupt. Can the author please tell what is wrong with the code below. The intent is to make a zip file ("test.zip"), and add "testfile.txt" into it:

CompressionInfo info = new CompressionInfo();
info.Type = CompressionType.Deflate;
var zipFile = File.OpenWrite("C:\\test.zip");
var testFile = File.OpenRead("C:\\testfile.txt");
var zipWriter = WriterFactory.Open(zipFile, ArchiveType.Zip, info);
zipWriter.Write("testfile.txt", testFile, DateTime.Now);
zipFile.Close();
testFile.Close();

tsiteb Oct 11, 2011 at 10:40 AM 
Unfortunately, I had trouble with several of these examples. The depth of sample code for SharpCompress is very shallow in these documents and on the web and I was forced to abandon usage for Windows Phone 7 after about three hours of testing. I see in one comment and the response that ZipArchive.Create was internal instead of public. I could not get the source code to load in my computer for Windows Phone 7 to compile it either. The project would not load in Visual Studio 2010. This is unfortunate...