Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-11-02
5
Medium Priority
?
417 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
Comment
Question by:smacca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:God_Ares
ID: 12471857
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
ID: 12471931
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
ID: 12480032
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
ID: 12488908
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:
thedude112286 earned 2000 total points
ID: 12513994
// 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

598 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