a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c#使用DotNetZip封裝類操作zip文件(創(chuàng)建/讀取/更新)實例

2020-01-24 03:03:19
字體:
供稿:網(wǎng)友

下載地址在這里:http://dotnetzip.codeplex.com/

下載到的包里有很多個dll文件,一般引用Ionic.Zip.dll就可以:

然后引用這個命名空間:

using Ionic.Zip;

以下是我自己封裝的一個類:

復(fù)制代碼 代碼如下:

/// <summary>
    /// Zip操作基于 DotNetZip 的封裝
    /// </summary>
    public static class ZipUtils
    {
        /// <summary>
        /// 得到指定的輸入流的ZIP壓縮流對象【原有流對象不會改變】
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
        {
            MemoryStream compressedStream = new MemoryStream();
            if (sourceStream != null)
            {
                long sourceOldPosition = 0;
                try
                {
                    sourceOldPosition = sourceStream.Position;
                    sourceStream.Position = 0;
                    using (ZipFile zip = new ZipFile())
                    {
                        zip.AddEntry(entryName, sourceStream);
                        zip.Save(compressedStream);
                        compressedStream.Position = 0;
                    }
                }
                catch
                {
                }
                finally
                {
                    try
                    {
                        sourceStream.Position = sourceOldPosition;
                    }
                    catch
                    {
                    }
                }
            }
            return compressedStream;
        }


        /// <summary>
        /// 得到指定的字節(jié)數(shù)組的ZIP解壓流對象
        /// 當(dāng)前方法僅適用于只有一個壓縮文件的壓縮包,即方法內(nèi)只取壓縮包中的第一個壓縮文件
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        public static Stream ZipDecompress(byte[] data)
        {
            Stream decompressedStream = new MemoryStream();
            if (data != null)
            {
                try
                {
                    MemoryStream dataStream = new MemoryStream(data);
                    using (ZipFile zip = ZipFile.Read(dataStream))
                    {
                        if (zip.Entries.Count > 0)
                        {
                            zip.Entries.First().Extract(decompressedStream);
                            // Extract方法中會操作ms,后續(xù)使用時必須先將Stream位置歸零,否則會導(dǎo)致后續(xù)讀取不到任何數(shù)據(jù)
                            // 返回該Stream對象之前進行一次位置歸零動作
                            decompressedStream.Position = 0;
                        }
                    }
                }
                catch
                {
                }
            }
            return decompressedStream;
        }

        /// <summary>
        /// 壓縮ZIP文件
        /// 支持多文件和多目錄,或是多文件和多目錄一起壓縮
        /// </summary>
        /// <param name="list">待壓縮的文件或目錄集合</param>
        /// <param name="strZipName">壓縮后的文件名</param>
        /// <param name="IsDirStruct">是否按目錄結(jié)構(gòu)壓縮</param>
        /// <returns>成功:true/失敗:false</returns>
        public static bool CompressMulti(List<string> list, string strZipName, bool IsDirStruct)
        {
            try
            {
                using (ZipFile zip = new ZipFile(Encoding.Default))//設(shè)置編碼,解決壓縮文件時中文亂碼
                {
                    foreach (string path in list)
                    {
                        string fileName = Path.GetFileName(path);//取目錄名稱
                        //如果是目錄
                        if (Directory.Exists(path))
                        {
                            if (IsDirStruct)//按目錄結(jié)構(gòu)壓縮
                            {
                                zip.AddDirectory(path, fileName);
                            }
                            else//目錄下的文件都壓縮到Zip的根目錄
                            {
                                zip.AddDirectory(path);
                            }
                        }
                        if (File.Exists(path))//如果是文件
                        {
                            zip.AddFile(path);
                        }
                    }
                    zip.Save(strZipName);//壓縮
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 解壓ZIP文件
        /// </summary>
        /// <param name="strZipPath">待解壓的ZIP文件</param>
        /// <param name="strUnZipPath">解壓的目錄</param>
        /// <param name="overWrite">是否覆蓋</param>
        /// <returns>成功:true/失敗:false</returns>
        public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
        {
            try
            {
                ReadOptions options = new ReadOptions();
                options.Encoding = Encoding.Default;//設(shè)置編碼,解決解壓文件時中文亂碼
                using (ZipFile zip = ZipFile.Read(strZipPath, options))
                {
                    foreach (ZipEntry entry in zip)
                    {
                        if (string.IsNullOrEmpty(strUnZipPath))
                        {
                            strUnZipPath = strZipPath.Split('.').First();
                        }
                        if (overWrite)
                        {
                            entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解壓文件,如果已存在就覆蓋
                        }
                        else
                        {
                            entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解壓文件,如果已存在不覆蓋
                        }
                    }
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }


    }

使用方法:

1.壓縮文件

復(fù)制代碼 代碼如下:

List<string> list = new List<string>();
  list.Add(@"D:/Test/ss");
  list.Add(@"D:/Test/test1.jpg");
  list.Add(@"D:/公司文件.txt");
  list.Add(@"D:/Test/ss.xml");
  bool isSuc =ZipUtils. CompressMulti(list, "D://Test2.zip",true);

2.解壓文件

復(fù)制代碼 代碼如下:

bool isSuc = ZipUtils.Decompression("D://Test//Test1.zip", "D://Teest", true);

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 狠狠插狠狠操 | 日韩欧美国产视频 | 欧美日韩精品一区二区 | 欧美日韩国产综合在线 | a天堂中文在线 | av国产精品 | 欧美一区三区 | 国产精品久久久久久久久久妞妞 | 范冰冰一级做a爰片久久毛片 | 九九九九精品 | 免费a网站 | 欧美精品一区在线发布 | 91精品国产91久久久久久吃药 | 四虎永久免费在线 | 成人一区二区在线播放 | 欧美爱爱网 | 亚洲乱码一区二区 | 日韩国产免费观看 | 日韩视频在线免费观看 | 久久亚洲一区 | 这里精品 | 日本一区二区精品视频 | 久久国产精品免费一区二区三区 | 男女视频在线观看 | 日韩五码在线 | 黄av在线 | 综合五月激情 | 午夜高清视频 | 免费av直接看 | 日韩一区二区三区在线播放 | 综合色播| 日本一区视频在线观看 | 久久久久成人精品 | 国产黄色免费 | 青青久久久 | 国产一区二区三区免费视频 | 毛片国产| 中文字幕一区二区在线观看 | 牛牛精品 | av一级毛片| 欧美日韩第一 |