Recommend a Library/API to Unzip File in C#

recommend a library/API to unzip file in C#

The GPL

http://www.icsharpcode.net/OpenSource/SharpZipLib/

OR the less restrictive Ms-PL

http://www.codeplex.com/DotNetZip

To complete this answer the .net framework has ZipPackage I had less success with it.

how to unzip the file using c#

Have a look at the GZipStream, it's one of the built-in zip support in the framework, there's an example on the MSDN page:
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

Here's the example from the MSDN page:

public class Program
{

public static void Main()
{
// Path to directory of files to compress and decompress.
string dirpath = @"c:\users\public\reports";

DirectoryInfo di = new DirectoryInfo(dirpath);

// Compress the directory's files.
foreach (FileInfo fi in di.GetFiles())
{
Compress(fi);
}

// Decompress all *.gz files in the directory.
foreach (FileInfo fi in di.GetFiles("*.gz"))
{
Decompress(fi);
}
}

public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and
// already compressed files.
if ((File.GetAttributes(fi.FullName)
& FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
// Create the compressed file.
using (FileStream outFile =
File.Create(fi.FullName + ".gz"))
{
using (GZipStream Compress =
new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);

Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}

public static void Decompress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension, for example
// "doc" from report.doc.gz.
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length -
fi.Extension.Length);

//Create the decompressed file.
using (FileStream outFile = File.Create(origName))
{
using (GZipStream Decompress = new GZipStream(inFile,
CompressionMode.Decompress))
{
// Copy the decompression stream
// into the output file.
Decompress.CopyTo(outFile);

Console.WriteLine("Decompressed: {0}", fi.Name);
}
}
}
}
}

Unzip files programmatically in .net

We have used SharpZipLib successfully on many projects. I know it's a third party tool, but source code is included and could provide some insight if you chose to reinvent the wheel here.

How to unzip compressed files using C#?

The System.IO.Packaging namespace contains some compression classes such as ZipPackage but they are by no means as simple as using a third party libraries. I strongly recommend you to consider a third party library.

Extracting files from a Zip archive programmatically using C# and System.IO.Packaging

If you are manipulating ZIP files, you may want to look into a 3rd-party library to help you.

For example, DotNetZip, which has been recently updated. The current version is now v1.8. Here's an example to create a zip:

using (ZipFile zip = new ZipFile())
{
zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
zip.AddFile("ReadMe.txt");

zip.Save("Archive.zip");
}

Here's an example to update an existing zip; you don't need to extract the files to do it:

using (ZipFile zip = ZipFile.Read("ExistingArchive.zip"))
{
// 1. remove an entry, given the name
zip.RemoveEntry("README.txt");

// 2. Update an existing entry, with content from the filesystem
zip.UpdateItem("Portfolio.doc");

// 3. modify the filename of an existing entry
// (rename it and move it to a sub directory)
ZipEntry e = zip["Table1.jpg"];
e.FileName = "images/Figure1.jpg";

// 4. insert or modify the comment on the zip archive
zip.Comment = "This zip archive was updated " + System.DateTime.ToString("G");

// 5. finally, save the modified archive
zip.Save();
}

here's an example that extracts entries:

using (ZipFile zip = ZipFile.Read("ExistingZipFile.zip"))
{
foreach (ZipEntry e in zip)
{
e.Extract(TargetDirectory, true); // true => overwrite existing files
}
}

DotNetZip supports multi-byte chars in filenames, Zip encryption, AES encryption, streams, Unicode, self-extracting archives.
Also does ZIP64, for file lengths greater than 0xFFFFFFFF, or for archives with more than 65535 entries.

free. open source

get it at
codeplex or direct download from windows.net - CodePlex has been discontinued and archived

How to extract ZIP file in C#

DotNetZip:

class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files...

DotNetZip works on PCs with the full .NET Framework, and also runs on mobile devices that use the .NET Compact Framework. Create and read zip files in VB, C#, or any .NET language, or any scripting environment. DotNetZip supports these scenarios:

  • a Silverlight app that dynamically creates zip files.
  • an ASP.NET app that dynamically creates ZIP files and allows a browser to download them
  • a Windows Service that periodically zips up a directory for backup and archival purposes
  • a WPF program that modifies existing archives - renaming entries, removing entries from an archive, or adding new entries to an archive
  • a Windows Forms app that creates AES-encrypted zip archives for privacy of archived content.
  • a SSIS script that unzips or zips
  • An administrative script in PowerShell or VBScript that performs backup and archival.
  • a WCF service that receives a zip file as an attachment, and dynamically unpacks the zip to a stream for analysis
  • an old-school ASP (VBScript) application that produces a ZIP file via the COM interface for DotNetZIp
  • a Windows Forms app that reads or updates ODS files
  • creating zip files from stream content, saving to a stream, extracting to a stream, reading from a stream
  • creation of self-extracting archives.

If all you want is a better DeflateStream or GZipStream class to replace the one that is built-into the .NET BCL, DotNetZip has that, too. DotNetZip's DeflateStream and GZipStream are available in a standalone assembly, based on a .NET port of Zlib. These streams support compression levels and deliver much better performance than the built-in classes. There is also a ZlibStream to complete the set (RFC 1950, 1951, 1952)...

have fun

Unzip a file with a particular extension (not .zip)

The problem is that a .ZIP file is much more than simply deflated data. There's directory structures, checksums, file metadata, etc. etc.

You need to use a class that knows about this structure. Unless the file is using some of the more advanced stuff, such as encryption and spanning archives, the .NET ZipArchive class probably does the trick.

Here's a simple program that extracts the contents of a text file from the zip archive. You must adapt it to your needs:

using (var file = File.Open(@"D:\Temp\Temp.zip", FileMode.Open))
using (var archive = new ZipArchive(file))
{
var entry = archive.GetEntry("ttt/README.md");
using (var entryStream = entry.Open())
using (var memory = new MemoryStream())
{
entryStream.CopyTo(memory);
Console.WriteLine(Encoding.UTF8.GetString(memory.ToArray()));
}
}

Unzip folder reccursively in Android Xamarin mono 3.2.6

If it is throwing error in Release mode then it may be because of the linker. To zip and unzip files you can also use

java.util.zip

package. More info here.

Edit: Sample code

include namespace using Java.Util.Zip;

using ( ZipInputStream s = new ZipInputStream ( File.OpenRead ( strSourcePath ) ) )
{
ZipEntry theEntry;
while ( ( theEntry = s.NextEntry ) != null )
{
string directoryName = Path.GetDirectoryName ( theEntry.Name );
string fileName = Path.GetFileName ( theEntry.Name );
directoryName = Path.Combine ( strDestFolderPath , directoryName );
if ( directoryName.Length > 0 )
{
Directory.CreateDirectory ( directoryName );
}
if ( fileName != String.Empty )
{
using ( FileStream streamWriter = File.Create ( Path.Combine ( strDestFolderPath , theEntry.Name ) ) )
{
int size = 2048;
byte [] data = new byte[size];
while ( true )
{
size = s.Read ( data , 0 , data.Length );
if ( size > 0 )
{
streamWriter.Write ( data , 0 , size );
}
else
{
break;
}
}
}
}
}
}

where strSourcePath is source zip file path and strDestFolderPath is destination folder path



Related Topics



Leave a reply



Submit