views:

374

answers:

6

Hi All

I have seen many tutorials on how to compress a single file in c#. But I need to be able to create a normal *.zip file out of more than just one file. Is there anything in .NET that can do this? What would you suggest (baring in mind I'm under strict rules and cannot use other libraries)

Thank you

+6  A: 

You can use Windows' shell functions as outlined here

Stuart Dunkeld
Thanks @Stuart. Much appreciated!
lucifer
+2  A: 

There is nothing built into the .NET Framework that lets you easily zip/unzip several files or directories into an archive. I looked for quite a while, before resorting to use DotNetZip.

Jens
I can't use any third-party stuff unfortunately, but thanks heaps for the link as I will be using it for personal projects. much appreciated. :)
lucifer
There is a namespace called System.IO.Compression using this namespace you can easily implement zipping and unzipping files in a folder.
gyurisc
"I can't use any third-party stuff" I assume this was a business decision dictated to you? What idiots come up with these ideas that every wheel needs reinvented atleast this has a parallel for internal .NET though.
Chris Marisic
@AskAboutGadgets - you are **incorrect**. There is a System.IO.Compression namespace, but there is no class in that namespace that handles (reads or writes) ZIP files. The main classes are GZipStream and DeflateStream, neither of which manipulate zip files. The docs clearly state this. Also, it is not trivial or "easy" to augment these classes with the necessary logic to read or write zip files.
Cheeso
@Cheeso Thanks for the correction.
gyurisc
+2  A: 

You can try SharpZipLib for that. Is is open source, platform independent pure c# code.

codymanix
+1  A: 

.NET has a built functionality for compressing files in the System.IO.Compression namespace. Using this you do not have to take an extra library as a dependency. This functionality is available from .NET 2.0.

Here is the way to do the compressing from the MSDN page I linked:

    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.
                        byte[] buffer = new byte[4096];
                        int numRead;
                        while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            Compress.Write(buffer, 0, numRead);
                        }
                        Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                            fi.Name, fi.Length.ToString(), outFile.Length.ToString());
                    }
                }
            }
        }
gyurisc
gzip != 'normal' zip file
Arnis L.
+2  A: 

You should look into Zip Packages

They are a more structured version of normal ZIP archives, requiring some meta stuff in the root. So other ZIP tools can open a package, but the Sysytem.IO.Packaging API can not open all ZIP files.

Henk Holterman
+2  A: 

Here are a few resources you might consider: Creating Zip archives in .NET (without an external library like SharpZipLib)

Zip Your Streams with System.IO.Packaging

My recommendation and preference would be to use system.io.packacking. This keeps your dependencies down (just the framework). Jgalloway’s post (the first reference) provides a good example of adding two files to a zip file. Yes, it is more verbose, but you can easily create a façade (to a degree his AddFileToZip does that).

HTH

Jake