first ImportNamespaces
using System.IO; using System.IO.Compression;
Add This Code For Zipping -------------------------------
FileStream objFSIN=null; FileStream objFsOp=null; GZipStream objGZ=null; byte[] btArr; int intCount = 0; try { objFsOp = new FileStream("C:\\Zip\\New\\Desktop\\New Folder\\AB.zip", FileMode.Create, FileAccess.Write, FileShare.None); objGZ = new GZipStream(objFsOp,CompressionMode.Compress,true); objFSIN = new FileStream("D:\\AB.xml", FileMode.Open, FileAccess.Read, FileShare.Read); btArr = new byte[objFSIN.Length]; intCount=objFSIN.Read(btArr,0,btArr.Length); // objFSIN.Flush(); //objFSIN.Close(); objGZ.Write(btArr,0,btArr.Length);
} catch {
} finally { if (objGZ != null) { objGZ.Flush(); objGZ.Close(); objGZ = null;
} if (objFsOp != null) { objFsOp.Flush(); objFsOp.Close();
} if (objFSIN != null) { objFSIN.Flush(); objFSIN.Close();
} }
Add this code For Unzipping -----------------------------------------------
FileStream fsin = null; FileStream fsop = null; GZipStream objgz=null; const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int count = 0; try { fsin = new FileStream("C:\\Zip\\New\\Desktop\\New Folder\\AB.zip", FileMode.Open, FileAccess.Read, FileShare.Read); fsop = new FileStream("C:\\Zip\\New\\Desktop\\New Folder\\AB.xml", FileMode.Create, FileAccess.Write, FileShare.None); objgz = new GZipStream(fsin, CompressionMode.Decompress, true); while (true) {
count = objgz.Read(buffer, 0, bufferSize);
if (count != 0) {
fsop.Write(buffer, 0, count);
}
if (count != bufferSize) {
// have reached the end
break;
}
}
} catch {
} finally { if (objgz != null) {
objgz.Close();
objgz = null;
}
if (fsop != null) {
fsop.Close();
fsop = null;
}
if (fsin != null) {
fsin.Close();
fsin = null;
} }
|
| Author: Payal Jain 27 May 2008 | Member Level: Gold Points : 2 |
throwing exception when i tried this code :(
|