Solved

How can I unzip a zip file on the server programatically?

Posted on 2004-11-02
392 Views
Last Modified: 2010-04-15
I have a zip file that I am uploading to server.
It contains a host of Windows Media Audio Files.

I need to unzip this file programatically.
Is this possible?
If so, can you provide a soution.

Cheers.
0
Question by:smacca
    5 Comments
     
    LVL 7

    Expert Comment

    by:God_Ares
    this requires a program/service that is looking at a dir (filesystemwatcher) and if a new .zip file has been uploaded unzip it.



    filesystem stuff

    http://www.codeproject.com/csharp/fswatcher.asp

    zip stuff

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


    regards

    Floris
    0
     
    LVL 7

    Expert Comment

    by:God_Ares
    plz provide more infromation, eg, where are the files being unziped to?,... what is going to happenb with the original zip file etc.
    0
     
    LVL 4

    Expert Comment

    by:thedude112286
    Upload the file and then use SharpZipLib to unzip.  I use this library everytime I need file zipping.  You can find it at www.icsharpcode.net.  If you need any extra help with this just ask.
    0
     

    Author Comment

    by:smacca
    I have downloaded SharpZipLib but cannot determine how to unzip from the API docs.
    Can you provide any code examples of the unzip process?
    0
     
    LVL 4

    Accepted Solution

    by:
    // this code block needs 2 variables: destination (path to the destination zip file) and file (path to the source file)
    // you could add more ZipEntrys to zip multiple files or directories

    using ICSharpCode.SharpZipLib.Zip;

    int COMPRESSION_LEVEL = 1;

    ZipOutputStream zipOut = new ZipOutputStream(File.Create(destination));
    zipOut.SetLevel(COMPRESSION_LEVEL);

    ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();

    System.IO.FileStream fs = System.IO.File.OpenRead(file);                  
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    ZipEntry entry = new ZipEntry(file.Substring(startingPath.Length-1)); // makes file path relative

    //this block forces compressed file type to be stored
    // not much sense in trying to compress further
    // more types should be added and more elegantly
    bool compressedFileType = false;
    string fileType = file.ToUpper();
    if (fileType.EndsWith(".ZIP") || fileType.EndsWith(".RAR") ||fileType.EndsWith(".GZ")  || fileType.EndsWith(".LZH") ||
         fileType.EndsWith(".PAK") || fileType.EndsWith(".CAB"))
         compressedFileType = true;

    if (!compressedFileType)
        entry.CompressionMethod = CompressionMethod.Deflated; //deflate
    else
        entry.CompressionMethod = CompressionMethod.Stored; //store

    entry.DateTime = DateTime.Now;

    // set Size and the crc, because the information
    // about the size and crc should be stored in the header
    // if it is not set it is automatically written in the footer.
    // (in this case size == crc == -1 in the header)
    // Some ZIP programs have problems with zip files that don't store
    // the size and crc in the header.
    entry.Size = fs.Length;                        
    fs.Close();                  
    crc.Reset();
    crc.Update(buffer);                  
    entry.Crc  = crc.Value;                  
    s.PutNextEntry(entry);
    s.Write(buffer, 0, buffer.Length);

    zipOut.Finish();
    zipOut.Close();
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now