Unzip Files Programmatically in .Net

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 can I unzip a file to a .NET memory stream?

Zip compression support is built in:

using System.IO;
using System.IO.Compression;
// ^^^ requires a reference to System.IO.Compression.dll
static class Program
{
const string path = ...
static void Main()
{
using(var file = File.OpenRead(path))
using(var zip = new ZipArchive(file, ZipArchiveMode.Read))
{
foreach(var entry in zip.Entries)
{
using(var stream = entry.Open())
{
// do whatever we want with stream
// ...
}
}
}
}
}

Normally you should avoid copying it into another stream - just use it "as is", however, if you absolutely need it in a MemoryStream, you could do:

using(var ms = new MemoryStream())
{
stream.CopyTo(ms);
ms.Position = 0; // rewind
// do something with ms
}

How to extract zip file contents into a folder in .NET 4.5

You will need to add a reference to the System.IO.Compression.FileSystem assembly.

Every library class has an MSDN page. This is the one for ZipFile.

Notice the specification of the namespace and the assembly in the top section.

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 created zip folder to same folder?

If your file is a .zip file that should work. I tried this in a rar file that gave me error [System.IO.Invalid Data Exception] for checking that error you can go to that link.

End of Central Directory record could not be found

You are trying to unzip a file(.zip extension) to a File folder. That is what i understood from your question but in your code you gave a file location which has a .zip extension. It needs to be a File folder.
I made some changes check this one.

namespace ZipWorker
{
class Program
{
static void Main()
{
string pathInput;
string pathString;


do
{
Console.WriteLine("Please enter the path for the folder that you want to create");
pathString = Console.ReadLine();

} while (String.IsNullOrEmpty(pathString));


do
{
Console.WriteLine("Please enter the path for the Folder to copy");
pathInput = Console.ReadLine();

} while (String.IsNullOrEmpty(pathInput));


System.IO.Directory.CreateDirectory(pathString);


string fileName = System.IO.Path.GetFileName(pathInput);
if (fileName.Contains(".zip"))
{
fileName = fileName.Replace(".zip", "");
}

pathString = System.IO.Path.Combine(pathString, fileName) ;


Console.WriteLine("Path to my file: {0}\n", pathString);

if (!System.IO.Directory.Exists(pathString))
{
System.IO.Directory.CreateDirectory(pathString);
ZipFile.ExtractToDirectory(pathInput, pathString);

}
else
{
Console.WriteLine("File \"{0}\" already exists.", fileName);
return;
}
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}


Related Topics



Leave a reply



Submit