zoukankan      html  css  js  c++  java
  • ICSharpCode.SharpZipLib压缩解压

    一、使用ICSharpCode.SharpZipLib.dll;  

      下载地址  

      http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx  

     

    二、基于(ICSharpCode.SharpZipLib.dll)的文件压缩方法,类文件

    压缩文件

     

    1. using System;  
    2. using System.IO;  
    3. using System.Collections;  
    4. using ICSharpCode.SharpZipLib.Checksums;  
    5. using ICSharpCode.SharpZipLib.Zip;  
    6.   
    7.   
    8. namespace FileCompress  
    9. {  
    10.     /// <summary>  
    11.     /// 功能:压缩文件  
    12.     /// creator chaodongwang 2009-11-11  
    13.     /// </summary>  
    14.     public class ZipClass  
    15.     {  
    16.         /// <summary>  
    17.         /// 压缩单个文件  
    18.         /// </summary>  
    19.         /// <param name="FileToZip">被压缩的文件名称(包含文件路径)</param>  
    20.         /// <param name="ZipedFile">压缩后的文件名称(包含文件路径)</param>  
    21.         /// <param name="CompressionLevel">压缩率0(无压缩)-9(压缩率最高)</param>  
    22.         /// <param name="BlockSize">缓存大小</param>  
    23.         public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel)  
    24.         {  
    25.             //如果文件没有找到,则报错   
    26.             if (!System.IO.File.Exists(FileToZip))  
    27.             {  
    28.                 throw new System.IO.FileNotFoundException("文件:" + FileToZip + "没有找到!");  
    29.             }  
    30.   
    31.             if (ZipedFile == string.Empty)  
    32.             {  
    33.                 ZipedFile = Path.GetFileNameWithoutExtension(FileToZip) + ".zip";  
    34.             }  
    35.   
    36.             if (Path.GetExtension(ZipedFile) != ".zip")  
    37.             {  
    38.                 ZipedFile = ZipedFile + ".zip";  
    39.             }  
    40.   
    41.             ////如果指定位置目录不存在,创建该目录  
    42.             //string zipedDir = ZipedFile.Substring(0,ZipedFile.LastIndexOf("/"));  
    43.             //if (!Directory.Exists(zipedDir))  
    44.             //    Directory.CreateDirectory(zipedDir);  
    45.   
    46.             //被压缩文件名称  
    47.             string filename = FileToZip.Substring(FileToZip.LastIndexOf('//') + 1);  
    48.               
    49.             System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);  
    50.             System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);  
    51.             ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);  
    52.             ZipEntry ZipEntry = new ZipEntry(filename);  
    53.             ZipStream.PutNextEntry(ZipEntry);  
    54.             ZipStream.SetLevel(CompressionLevel);  
    55.             byte[] buffer = new byte[2048];  
    56.             System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);  
    57.             ZipStream.Write(buffer, 0, size);  
    58.             try  
    59.             {  
    60.                 while (size < StreamToZip.Length)  
    61.                 {  
    62.                     int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);  
    63.                     ZipStream.Write(buffer, 0, sizeRead);  
    64.                     size += sizeRead;  
    65.                 }  
    66.             }  
    67.             catch (System.Exception ex)  
    68.             {  
    69.                 throw ex;  
    70.             }  
    71.             finally  
    72.             {  
    73.                 ZipStream.Finish();  
    74.                 ZipStream.Close();  
    75.                 StreamToZip.Close();  
    76.             }  
    77.         }  
    78.   
    79.         /// <summary>  
    80.         /// 压缩文件夹的方法  
    81.         /// </summary>  
    82.         public void ZipDir(string DirToZip, string ZipedFile, int CompressionLevel)  
    83.         {  
    84.             //压缩文件为空时默认与压缩文件夹同一级目录  
    85.             if (ZipedFile == string.Empty)  
    86.             {  
    87.                 ZipedFile = DirToZip.Substring(DirToZip.LastIndexOf("/") + 1);  
    88.                 ZipedFile = DirToZip.Substring(0, DirToZip.LastIndexOf("/")) +"//"+ ZipedFile+".zip";  
    89.             }  
    90.   
    91.             if (Path.GetExtension(ZipedFile) != ".zip")  
    92.             {  
    93.                 ZipedFile = ZipedFile + ".zip";  
    94.             }  
    95.   
    96.             using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(ZipedFile)))  
    97.             {  
    98.                 zipoutputstream.SetLevel(CompressionLevel);  
    99.                 Crc32 crc = new Crc32();  
    100.                 Hashtable fileList = getAllFies(DirToZip);  
    101.                 foreach (DictionaryEntry item in fileList)  
    102.                 {  
    103.                     FileStream fs = File.OpenRead(item.Key.ToString());  
    104.                     byte[] buffer = new byte[fs.Length];  
    105.                     fs.Read(buffer, 0, buffer.Length);  
    106.                     ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(DirToZip.Length + 1));  
    107.                     entry.DateTime = (DateTime)item.Value;  
    108.                     entry.Size = fs.Length;  
    109.                     fs.Close();  
    110.                     crc.Reset();  
    111.                     crc.Update(buffer);  
    112.                     entry.Crc = crc.Value;  
    113.                     zipoutputstream.PutNextEntry(entry);  
    114.                     zipoutputstream.Write(buffer, 0, buffer.Length);  
    115.                 }  
    116.             }  
    117.         }  
    118.   
    119.         /// <summary>  
    120.         /// 获取所有文件  
    121.         /// </summary>  
    122.         /// <returns></returns>  
    123.         private Hashtable getAllFies(string dir)  
    124.         {  
    125.             Hashtable FilesList = new Hashtable();  
    126.             DirectoryInfo fileDire = new DirectoryInfo(dir);  
    127.             if (!fileDire.Exists)  
    128.             {  
    129.                 throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!");  
    130.             }  
    131.   
    132.             this.getAllDirFiles(fileDire, FilesList);  
    133.             this.getAllDirsFiles(fileDire.GetDirectories(), FilesList);  
    134.             return FilesList;  
    135.         }  
    136.         /// <summary>  
    137.         /// 获取一个文件夹下的所有文件夹里的文件  
    138.         /// </summary>  
    139.         /// <param name="dirs"></param>  
    140.         /// <param name="filesList"></param>  
    141.         private void getAllDirsFiles(DirectoryInfo[] dirs, Hashtable filesList)  
    142.         {  
    143.             foreach (DirectoryInfo dir in dirs)  
    144.             {  
    145.                 foreach (FileInfo file in dir.GetFiles("*.*"))  
    146.                 {  
    147.                     filesList.Add(file.FullName, file.LastWriteTime);  
    148.                 }  
    149.                 this.getAllDirsFiles(dir.GetDirectories(), filesList);  
    150.             }  
    151.         }  
    152.         /// <summary>  
    153.         /// 获取一个文件夹下的文件  
    154.         /// </summary>  
    155.         /// <param name="strDirName">目录名称</param>  
    156.         /// <param name="filesList">文件列表HastTable</param>  
    157.         private void getAllDirFiles(DirectoryInfo dir, Hashtable filesList)  
    158.         {  
    159.             foreach (FileInfo file in dir.GetFiles("*.*"))  
    160.             {  
    161.                 filesList.Add(file.FullName, file.LastWriteTime);  
    162.             }  
    163.         }  
    164.     }  
    165. }  

     

        解压文件

    1. using System;  
    2. using System.Collections.Generic;  
    3. /// <summary>   
    4. /// 解压文件   
    5. /// </summary>   
    6.   
    7. using System;  
    8. using System.Text;  
    9. using System.Collections;  
    10. using System.IO;  
    11. using System.Diagnostics;  
    12. using System.Runtime.Serialization.Formatters.Binary;  
    13. using System.Data;  
    14.   
    15. using ICSharpCode.SharpZipLib.Zip;  
    16. using ICSharpCode.SharpZipLib.Zip.Compression;  
    17. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;   
    18.   
    19. namespace FileCompress  
    20. {  
    21.     /// <summary>  
    22.     /// 功能:解压文件  
    23.     /// creator chaodongwang 2009-11-11  
    24.     /// </summary>  
    25.     public class UnZipClass  
    26.     {  
    27.         /// <summary>  
    28.         /// 功能:解压zip格式的文件。  
    29.         /// </summary>  
    30.         /// <param name="zipFilePath">压缩文件路径</param>  
    31.         /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  
    32.         /// <param name="err">出错信息</param>  
    33.         /// <returns>解压是否成功</returns>  
    34.         public void UnZip(string zipFilePath, string unZipDir)  
    35.         {  
    36.             if (zipFilePath == string.Empty)  
    37.             {  
    38.                 throw new Exception("压缩文件不能为空!");  
    39.             }  
    40.             if (!File.Exists(zipFilePath))  
    41.             {  
    42.                 throw new System.IO.FileNotFoundException("压缩文件不存在!");  
    43.             }  
    44.             //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  
    45.             if (unZipDir == string.Empty)  
    46.                 unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));  
    47.             if (!unZipDir.EndsWith("/"))  
    48.                 unZipDir += "/";  
    49.             if (!Directory.Exists(unZipDir))  
    50.                 Directory.CreateDirectory(unZipDir);  
    51.   
    52.             using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))  
    53.             {  
    54.   
    55.                 ZipEntry theEntry;  
    56.                 while ((theEntry = s.GetNextEntry()) != null)  
    57.                 {  
    58.                     string directoryName = Path.GetDirectoryName(theEntry.Name);  
    59.                     string fileName = Path.GetFileName(theEntry.Name);  
    60.                     if (directoryName.Length > 0)  
    61.                     {  
    62.                         Directory.CreateDirectory(unZipDir + directoryName);  
    63.                     }  
    64.                     if (!directoryName.EndsWith("/"))  
    65.                         directoryName += "/";  
    66.                     if (fileName != String.Empty)  
    67.                     {  
    68.                         using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))  
    69.                         {  
    70.   
    71.                             int size = 2048;  
    72.                             byte[] data = new byte[2048];  
    73.                             while (true)  
    74.                             {  
    75.                                 size = s.Read(data, 0, data.Length);  
    76.                                 if (size > 0)  
    77.                                 {  
    78.                                     streamWriter.Write(data, 0, size);  
    79.                                 }  
    80.                                 else  
    81.                                 {  
    82.                                     break;  
    83.                                 }  
    84.                             }  
    85.                         }  
    86.                     }  
    87.                 }  
    88.             }  
    89.         }  
    90.     }   
  • 相关阅读:
    面试题58 二叉树的下一个结点
    面试题57 删除链表中重复的结点
    面试题56 链表中环的入口结点
    面试题55 字符流中第一个不重复的字符
    面试题54 表示数值的字符串
    面试题50 树中两个结点的最低公共祖先
    面试题53 正则表达式匹配
    面试题52 构建乘积数组
    面试题51 数组中重复的数字
    Qt链接库出错version Qt_5 not defined
  • 原文地址:https://www.cnblogs.com/siaslfslovewp/p/3656512.html
Copyright © 2011-2022 走看看