none
how to unzip a file in .net 2.0 with C# as a language RRS feed

  • Question

  • I have a zip file.I want's to unzip it how can i do this in .net 2.0 as we know there is a class i think gzip from which i can unzip a file.
    Please can anybody help ?
    Thursday, February 1, 2007 11:28 AM

Answers

All replies

  • does #ziplib fit your licensing model?
    Thursday, February 1, 2007 11:51 AM
  • there is also a 'compression application sample' in the msdn library if you can find the link
    Thursday, February 1, 2007 11:56 AM
  • No #ziplib doesn't fit in my licensing model.
    I have downloaded the "
    compression application sample" and run that sample but it is a full application and there is some sort of .xip file extension which they are uncompressing.I can't well understood how can i get benefit from this application.
    I need a simple sample which will unzip a .zip file and save that file to some location [my .zip file contains only one xml file with the same name as the zip file]
    I have to do it in pure .net without using any third part dll.
    Previously in .net 1.1 I were using #ziplib for unziping file and ipworks for ftp download.
    Now as both of these features are supported in .net 2.0 {I doesn't say new as .net 3.0 is already released :) } so I want's to get rid of these third party libraries.
    So please can any body help me.
    Thursday, February 1, 2007 12:19 PM
  • i see.  how bout
    http://geekswithblogs.net/podwysocki/archive/2006/06/21/82628.aspx

    Thursday, February 1, 2007 12:29 PM
  • Thanks gabegade.

    But It still not solve my problem. I have a simple .zip file which i want's to extract.In the given logic it is first of all gor gzip and then name of the file is also given what can i do ?

    Friday, February 2, 2007 5:29 AM
  • Thanks gabegabe.

    But can't able to solve my problem.First of It needs to provide the name of the file secondly it uses gzip format.

    I simply want's to unzip a file [with .zip extension] using .net 2.0 classes.Can I do it ?

    Friday, February 2, 2007 6:39 AM
  • Any Idea ?
    Saturday, February 3, 2007 5:14 PM
  • Hi Kamran,

    The short answer is that the .NET libraries don't provide a way to easily unzip a zip-formatted file at the moment. And working around this takes a bit of effort. First I'll give you an overview of compression in .NET and then give you pointers to possible workarounds.

    As of now, the .NET compression libraries support at the core only one compression method, which is Deflate. Our GzipStream class, for example, is Deflate plus some extra header info and checksums -- i.e. the GZip format defined in RFC 1952. Many other compressed file formats besides gzip also use/accept Deflate as a compression method; such as zlib and zip. And the principle is the same in general for compressing a file: take the byte array generated by our DeflateStream class, and tack on some header bytes and checksums specific to that format, and you’re good to go.

    This makes it appealing to try to work with zip files via .NET's DeflateStream. I.e. you could create a class that reads the header info and then uses .NET deflate stream to do the compression work. Note that this is still a bit of work and involves understanding the zip format specification: http://www.pkware.com/documents/casestudies/APPNOTE.TXT

    The problem is going the reverse direction: decompressing the file gets trickier. According to the zip specification, other compression methods besides deflate are allowed. By reading the header bytes, you can determine which compression method is used. However, since the .NET libraries only support the deflate compression method at the moment, our classes wouldn't help with decompressing if the compression method is something besides deflate.

    About possible workarounds, there's an MSDN article describing working around this in C# using the J# libraries: http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/. Another option is to use a command line zip/unzip utility and call this from the System.Diagnostics.Process class from your program.

    This request has come up a few times and we're looking into releasing such support in the future, especially since the workarounds aren't very convenient.

    Thanks,

    Kim

    Saturday, March 24, 2007 10:05 PM
    Moderator
  • A few times?  The most often quoted solution is the Open Source library SharpZipLib.  Zip archive support is actually available in the framework, just not documented in the MSDN library.
    Sunday, March 25, 2007 12:09 AM
    Moderator
  • One thing i want's to ask is that do i have to also install J# redistributable or just .net framework is enough
    Sunday, March 25, 2007 3:22 PM
  • nobugz -- it's just a manner of speech Smile

     

    kamran, yes you need the j# redistributable. We'd prefer users not to have to take a dependency on these libraries which is why we'd like to have a replacement in the framework libraries.

    Friday, March 30, 2007 6:14 AM
    Moderator
  • And This will be implemented in .net framework   3.51 or 3.6   
    Friday, March 30, 2007 9:52 AM
  • a simple alternative is DotNetZip:
    http://www.codeplex.com/DotNetZip

       try 
       {  
         using (ZipFile zip = new ZipFile("MyZipFile.zip")  
         {  
           // add this file into the "images" directory in the zip archive  
           zip.AddFile("c:\\photos\\personal\\7440-N49th.png""images");  
           // add this file into a different directory in the archive  
           zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf""files");  
           zip.AddFile("ReadMe.txt");  
           zip.Save();  
         }  
       }  
       catch (System.Exception ex1)  
       {  
         System.Console.Error.WriteLine("exception: " + ex1);  
       }  
     

    or
    Try   
         Using zip As ZipFile = New ZipFile("MyZipFile.zip")  
             zip.AddFile("c:\photos\personal\7440-N49th.png")  
             zip.AddFile("c:\Desktop\2005_Annual_Report.pdf")  
             zip.AddFile("ReadMe.txt")  
             zip.Save  
         End Using  
     Catch ex1 As Exception  
         Console.Error.WriteLine("exception: {0}", ex1.ToString)  
     End Try 
     

     

    FYI: There is no zipfile capability in .NET 3.5.

    • Proposed as answer by Omid H Wednesday, March 18, 2009 1:15 PM
    Sunday, December 21, 2008 5:26 AM