c#打包檔案解壓縮

來源:互聯網
上載者:User

標籤:isnull   zip   動作   檔案   catch   下載   target   ppa   mem   

 

首先要引用一下類庫:using Ionic.Zip;這個類庫可以到網上下載。

  下面對類庫使用的封裝方法:

得到指定的輸入資料流的ZIP壓縮流對象

        /// <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;            }

得到指定的位元組數組的ZIP解壓流對象

 

/// <summary>            /// 得到指定的位元組數組的ZIP解壓流對象            /// 當前方法僅適用於只有一個壓縮檔的壓縮包,即方法內只取壓縮包中的第一個壓縮檔            /// </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,後續使用時必須先將Stream位置歸零,否則會導致後續讀取不到任何資料                                // 返回該Stream對象之前進行一次位置歸零動作                                decompressedStream.Position = 0;                            }                        }                    }                    catch                    {                    }                }                return decompressedStream;            }

 

壓縮ZIP檔案

 

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

 

解壓ZIP檔案

 

/// <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;//設定編碼,解決解壓檔案時中文亂碼                    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;                }            }

 

以上動圖由“圖鬥羅”提供

 

c#打包檔案解壓縮

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.