《asp.net C#实现解压缩文件的方法_.docx》由会员分享,可在线阅读,更多相关《asp.net C#实现解压缩文件的方法_.docx(8页珍藏版)》请在三一文库上搜索。
1、 C#实现解压缩文件的方法_ 网校上线了! 点击进入 网校开发及拥有的课件范围涉及公务员、财会类、外语类、外贸类、学历类、 职业资格类、计算机类、建筑工程类、等9大类考试的在线网络培训辅导。 本文实例讲解并描述了 C#实现解压缩文件的方法。一共给大家介绍了三段代码,一个是简洁的解压缩单个zip文件,后一个可以解压批量的大量的但需要调用ICSharpCode.SharpZipLib.dll类了,最终一个比较实例可压缩也可以解压缩了分享给大家供大家参考。具体如下: 解压缩单个文件: 代码如下: using System.IO; using System.IO.Compression; string
2、 sourceFile=D:2.zip; string destinationFile=D:1.txt; private const long BUFFER_SIZE = 20480; / make sure the source file is there if (File.Exists ( sourceFile ) FileStream sourceStream = null; FileStream destinationStream = null; GZipStream decompressedStream = null; byte quartetBuffer = null; try /
3、 Read in the compressed source stream sourceStream = new FileStream ( sourceFile, FileMode.Open ); / Create a compression stream pointing to the destiantion stream decompressedStream = new DeflateStream ( sourceStream, CompressionMode.Decompress, true ); / Read the footer to determine the length of
4、the destiantion file quartetBuffer = new byte4; int position = (int)sourceStream.Length - 4; sourceStream.Position = position; sourceStream.Read ( quartetBuffer, 0, 4 ); sourceStream.Position = 0; int checkLength = BitConverter.ToInt32 ( quartetBuffer, 0 ); byte buffer = new bytecheckLength + 100; i
5、nt offset = 0; int total = 0; / Read the compressed data into the buffer while ( true ) int bytesRead = decompressedStream.Read ( buffer, offset, 100 ); if ( bytesRead = 0 ) break; offset += bytesRead; total += bytesRead; / Now write everything to the destination file destinationStream = new FileStr
6、eam ( destinationFile, FileMode.Create ); destinationStream.Write ( buffer, 0, total ); / and flush everyhting to clean out the buffer destinationStream.Flush ( ); catch ( ApplicationException ex ) Console.WriteLine(ex.Message, 解压文件时发生错误:); finally / Make sure we allways close all streams if ( sourc
7、eStream != null ) sourceStream.Close ( ); if ( decompressedStream != null ) decompressedStream.Close ( ); if ( destinationStream != null ) destinationStream.Close ( ); 批量解压缩(这需要调用一个解压缩类库。 ICSharpCode.SharpZipLib.dll) 代码如下: using System; using System.IO; using System.Collections.Generic; using System
8、.Text; using ICSharpCode.SharpZipLib.Zip; namespace ZipLib / summary / 解压缩类 / /summary public static class ZIP / summary / 解压ZIP文件包 / /summary / param name=strZipFileZIP文件路径/param / param name=strDir解压后的文件名目路径/param / returns是否解压胜利/returns public static bool unzipFiles(string strZipFile, string strD
9、ir) /推断ZIP文件是否存在 if (File.Exists(strZipFile) /推断名目是否存在 bool bUnzipDir = false; /推断是否需要创建名目 if (!Directory.Exists(strDir) bUnzipDir = (Directory.CreateDirectory(strDir) != null); else bUnzipDir = true; /假如解压名目存在 if (bUnzipDir) /获得ZIP数据流 ZipInputStream zipStream = new ZipInputStream(File.OpenRead(strZ
10、ipFile); if (zipStream != null) ZipEntry zipEntry = null; while (zipEntry = zipStream.GetNextEntry() != null) string strUnzipFile = strDir + / + zipEntry.Name; string strFileName = Path.GetFileName(strUnzipFile); string strDirName = Path.GetDirectoryName(strUnzipFile); /是否为解压名目 if (!string.IsNullOrE
11、mpty(strDirName) Directory.CreateDirectory(strDirName); /是否为解压文件 if (!string.IsNullOrEmpty(strFileName) /解压文件 FileStream unzipFileStream = new FileStream(strUnzipFile, FileMode.Create); if (unzipFileStream != null) byte buf = new byte2048; int size = 0; while (size = zipStream.Read(buf, 0, 2048) 0) unzipFileStream.Write(buf, 0, size); /关闭Stream unzipFileStream.Flush(); unzipFileStream.Close(); /关闭ZIP流 zipStream.Close(); /返回值 return true; return false; 更多信息请查看IT技术专栏 .