Link to home
Start Free TrialLog in
Avatar of fwsteal
fwsteal

asked on

asp.net 2.0 c# compress/decompress files into zip format

how does one in asp.net 2.0 c# compress/decompress files into zip format and back out of zip format?

1. take a file such as C:\compressiontests\myfile.txt
   this could be a file from any source, possibly one that a user uploads
2. zip it as myfile.zip and store in C:\compressiontests\com\myfile.zip
3. unzip it as myfile.txt and store in C:\compressiontests\decom\myfile.txt

code:
string myFile;
myFile = @"C:\compressiontests\myfile.txt";

//get filestream
FileStream filestream = new FileStream(myFile, FileMode.Create); //don't want to recreate it just get the file in myFile
//compress file
GZipStream compressStream = new GZipStream(filestream, CompressionMode.Compress);
//write with compressed file to location
StreamWriter w = new StreamWriter(@"C:\compressiontests\com\" + compressStream + ".zip");
// this is what I get = System.IO.Compression.GZipStream.zip not the actual file name myfile.zip
//flush & close
w.Flush();
w.Close();
filestream.Close();
       

//now decompress/unzip the file
FileStream dfilestream = new FileStream(@"C:\compressiontests\com\" + w, FileMode.Open);
//get = can't find file
GZipStream decompressStream = new GZipStream(dfilestream, CompressionMode.Decompress);
StreamReader r = new StreamReader(decompressStream);

Avatar of rstrader
rstrader

I believe that .NET framework 2.0 only supports raw/deflate compression for streams and does not support the Zip file format.

have you looked at using something like the free C# zip library from http://www.sharpziplib.org ?
ASKER CERTIFIED SOLUTION
Avatar of rstrader
rstrader

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial