• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

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

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
smacca
Asked:
smacca
  • 2
  • 2
1 Solution
 
God_AresCommented:
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
 
God_AresCommented:
plz provide more infromation, eg, where are the files being unziped to?,... what is going to happenb with the original zip file etc.
0
 
thedude112286Commented:
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
 
smaccaAuthor Commented:
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
 
thedude112286Commented:
// 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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now