How to compress to .zip in C# - c#

What do I need to compress to .zip (not .gzip) in C# and how do I go about it?
I just need a quick answer, more preferably a link?
Thanks

DotNetZip is a good option. (http://dotnetzip.codeplex.com/)
It is pretty easy and quick.
Here is an example from the site:
Zip:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save("MyZipFile.zip");
}
Extract:
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
zip.ExtractAll(TargetDirectory);
}

http://sharpdevelop.net/OpenSource/SharpZipLib/

Related

how to find an entity in zip file is file or directory in c#

I want to create content of a zip file in treeview, but my problem is how to recognize the content of zip file is file or directory without extract the files and then add them to tree?
would you mind help me in solving this?
This functionality is available out of the box in .Net Framework 4.5 and later. You have to use this library:
using System.IO.Compression;
And then you'll be able to do this:
string zipPath = #"c:\example\start.zip";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith('\'))
Console.WriteLine($"{entry.FullName} is a directory.");
else
Console.WriteLine($"{entry.FullName} is a file.");
}
}
See here for more details:
How to list the contents of a .zip folder in c#?

Zip multiple directories Using Ionic Zip Library

I'm trying to zip several folders with their content into one zip file using Ionic Zip Library. The code below did created a zip file, however the folders were not added to it.
private void ZipFolder(List <string> folders, string pathToSaveZipFile)
{
using (ZipFile zip = new ZipFile())
{
foreach (string itrFolder in folders)
{
zip.AddDirectory(itrFolder);
}
zip.Save(pathToSaveZipFile);
}
}
Thank you
AddDirectory(string) adds the files to the root, you need to use
zip.AddDirectory(itrFolder, new DirectoryInfo(itrFolder).Name);
The second argument specifies the folder name within the ZIP.

How to add a zip file to zip file using C# and DotNetZip

I am using DotNetZip (http://dotnetzip.codeplex.com/) to build Zip files on the fly. I would like to be able to create a ZIP file in code and then add it to another ZIP file in code. Will this work? My code is below. I am trying to use a MemoryStream to hold the internal zip file and then add that memory stream to the main zip file. I'm not sure if this is correct. When I try this, my internal zip file has zero bytes and it fails when I try to open up the resulting main zip file.
using (ZipFile _mainZip = new ZipFile())
{
using (ZipFile _internalZip = new ZipFile())
{
..add stuff to the internal zip..
MemoryStream _myMemoryStream = new MemoryStream();
_internalZip.Save(_myMemoryStream);
_mainZip.AddEntry("myTitle.zip", _myMemoryStream);
}
}
_mainZip.Save("myZip.zip");
It might be that the MemoryStream's position is at the end of the stream.
You could try to set it to the begining:
_myMemoryStream.Position = 0;
That might work.

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.(.exe file)

I want to extract a exe file. The exe file contain some files and folders. When I try to extract the file using winrar it gets extracted but when I am trying to extract the exe file using some examples I am getting this error:
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
I have used some samples and googled a lot for my problem but didn't get my answer, and I have used some libraries also.
I used this code but same error:
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);
}
}
}
}
That's because the .exe file is a self-extracting archive...
You should give DotNetZip a try. From the project's FAQ:
Does this library read self-extracting zip files?
Yes. DotNetZip can read self-extracting archives (SFX) generated by WinZip, and WinZip
can read SFX files generated by DotNetZip.
You can install it from Nuget easily.

Gzip a directory that has subdirectories using GZipStream class with C#?

This MSDN site has an example to gzip a file. Then, how can I gzip a whole directory with sub directories in it?
Since gzip only works on files, I suggest you tar your directory and then gzip the generated tar file.
You can use tar-cs or SharpZipLib to generate your tar file.
You can't !
GZip was created for file, not directories :)
gzip operates on a simgle stream. To create a multi-stream (multi-file) archive using the gzipstream you need to include your own index. Basicly, at its simplest you would write the file offsets to the beginning of the output stream and then when you read it back in you know where the boundaries are. This method would not be PKZIP compatible. To be compatible you would have to read and implement the ZIP format... or use something like SharpZip, or Zip.NET
You can zip the directory in pure .NET 3.0. Using SharpZipLib may not be desirable due to the modified GPL license.
First, you will need a reference to WindowsBase.dll.
This code will open or create a zip file, create a directory inside, and place the file in that directory. If you want to zip a folder, possibly containing sub-directories, you could loop through the files in the directory and call this method for each file. Then, you could depth-first search the sub-directories for files, call the method for each of those and pass in the path to create that hierarchy within the zip file.
public void AddFileToZip(string zipFilename, string fileToAdd, string destDir)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = "." + destDir + "\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
}
}
}
}
destDir could be an empty string, which would place the file directly in the zip.
Sources:
https://weblogs.asp.net/jongalloway/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib
https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the-zip-file-with-system-io-packaging

Categories

Resources