C#中压缩工具整理之二 : SharpZipLib

/***************By Jiangong SUN*****************/

/***************by garcon1986*****************/ 2, SharpZipLib 继续上一篇文章,这里列出两个解压zip文件的实例。 PS: 所列出的所有代码都已验证可用 //extract single file using (var fs = new FileStream(unpackZipFile, FileMode.Open, FileAccess.Read)) { using (var zf = new ZipFile(fs)) { var ze = zf.GetEntry("test.txt"); if (ze == null) { throw new ArgumentException("FAMILLE_EXTRACT_20100916_101029.txt not found in zip"); } using (var inStream = zf.GetInputStream(ze)) { if (ze != null) { if (ze.IsFile) { if (ze.Name != "") { string fName = unpackDirectory + "//" + ze.Name; if (!Directory.Exists(unpackDirectory)) { Directory.CreateDirectory(unpackDirectory); } if (File.Exists(fName)) { continue; } using (FileStream streamWriter = File.Create(fName)) { int size = 2048; byte[] newbyte = new byte[size]; while (true) { size = inStream.Read(newbyte, 0, newbyte.Length); if (size > 0) streamWriter.Write(newbyte, 0, size); else break; } streamWriter.Close(); } } else { Console.WriteLine("theEntry.name == null"); } } } inStream.Close(); } } } //works for extracting all the files in zip file using (ZipInputStream inStream = new ZipInputStream(File.OpenRead(unpackZipFile))) { ZipEntry theEntry; while ((theEntry = inStream.GetNextEntry()) != null) { if (theEntry.IsFile) { if (theEntry.Name != "") { string fName = unpackDirectory + "//" + theEntry.Name; //Console.WriteLine("fileName: {0}", fName); if (!Directory.Exists(unpackDirectory)) { Directory.CreateDirectory(unpackDirectory); } if (File.Exists(fName)) { continue; } using (FileStream streamWriter = File.Create(fName)) { int size = 2048; byte[] newbyte = new byte[size]; while (true) { size = inStream.Read(newbyte, 0, newbyte.Length); if (size > 0) streamWriter.Write(newbyte, 0, size); else break; } streamWriter.Close(); } } else { Console.WriteLine("theEntry.name == null"); } } } inStream.Close(); }

 

Reference:

http://dotnetslackers.com/_NET/re-58114_Working_with_Zip_files_Part_I_Uploading_and_Expandning.aspx
http://www.blogjava.net/bacoo/archive/2007/12/29/171617.html
http://stackoverflow.com/questions/328343/using-sharpziplib-to-unzip-specific-files
http://www.answerspice.com/c119/1699274/using-sharpziplib-to-unzip-specific-files
http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-f.aspx
http://www.hdut.com/code/C_JieYaHuoYaSuoWenJianJia.htm
http://www.airhh.com/index.php/archives/14

你可能感兴趣的:(C#,C#,null,null,工具,byte,byte,reference,reference)