c#內建類實現的多檔案壓縮和解壓

來源:互聯網
上載者:User

標籤:namespace   syn   sts   converter   target   功能   ppa   pfile   結構   

c#內建的System.IO.Compression命名空間下的壓縮類實現的多檔案壓縮和解壓功能,缺點是多檔案壓縮包的解壓只能調用自身的解壓方法,和現有的壓縮軟體不相容。下面的代碼沒有把多檔案的目錄結構加進去

using System;using System.Collections.Generic;using System.IO;using System.IO.Compression;namespace Test.Zip{    class CompressHelper    {        /// <summary>        /// 單檔案壓縮(產生的壓縮包和第三方的解壓軟體相容)        /// </summary>        /// <param name="sourceFilePath"></param>        /// <returns></returns>        public string CompressSingle(string sourceFilePath)        {            string zipFileName = sourceFilePath + ".gz";            using (FileStream sourceFileStream = new FileInfo(sourceFilePath).OpenRead())            {                using (FileStream zipFileStream = File.Create(zipFileName))                {                    using (GZipStream zipStream = new GZipStream(zipFileStream, CompressionMode.Compress))                    {                        sourceFileStream.CopyTo(zipStream);                    }                }            }            return zipFileName;        }        /// <summary>        /// 自訂多檔案壓縮(產生的壓縮包和第三方的壓縮檔解壓不相容)        /// </summary>        /// <param name="sourceFileList">檔案清單</param>        /// <param name="saveFullPath">壓縮包全路徑</param>        public void CompressMulti(string[] sourceFileList, string saveFullPath)        {            MemoryStream ms = new MemoryStream();            foreach (string filePath in sourceFileList)            {                Console.WriteLine(filePath);                if (File.Exists(filePath))                {                    string fileName = Path.GetFileName(filePath);                    byte[] fileNameBytes = System.Text.Encoding.UTF8.GetBytes(fileName);                    byte[] sizeBytes = BitConverter.GetBytes(fileNameBytes.Length);                    ms.Write(sizeBytes, 0, sizeBytes.Length);                    ms.Write(fileNameBytes, 0, fileNameBytes.Length);                    byte[] fileContentBytes = System.IO.File.ReadAllBytes(filePath);                    ms.Write(BitConverter.GetBytes(fileContentBytes.Length), 0, 4);                    ms.Write(fileContentBytes, 0, fileContentBytes.Length);                }            }            ms.Flush();            ms.Position = 0;            using (FileStream zipFileStream = File.Create(saveFullPath))            {                using (GZipStream zipStream = new GZipStream(zipFileStream, CompressionMode.Compress))                {                    ms.Position = 0;                    ms.CopyTo(zipStream);                }            }            ms.Close();        }        /// <summary>        /// 多檔案壓縮解壓        /// </summary>        /// <param name="zipPath">壓縮檔路徑</param>        /// <param name="targetPath">解壓目錄</param>        public void DeCompressMulti(string zipPath, string targetPath)        {            byte[] fileSize = new byte[4];            if (File.Exists(zipPath))            {                using (FileStream fStream = File.Open(zipPath, FileMode.Open))                {                    using (MemoryStream ms = new MemoryStream())                    {                        using (GZipStream zipStream = new GZipStream(fStream, CompressionMode.Decompress))                        {                            zipStream.CopyTo(ms);                        }                        ms.Position = 0;                        while (ms.Position != ms.Length)                        {                            ms.Read(fileSize, 0, fileSize.Length);                            int fileNameLength = BitConverter.ToInt32(fileSize, 0);                            byte[] fileNameBytes = new byte[fileNameLength];                            ms.Read(fileNameBytes, 0, fileNameBytes.Length);                            string fileName = System.Text.Encoding.UTF8.GetString(fileNameBytes);                            string fileFulleName = targetPath + fileName;                            ms.Read(fileSize, 0, 4);                            int fileContentLength = BitConverter.ToInt32(fileSize, 0);                            byte[] fileContentBytes = new byte[fileContentLength];                            ms.Read(fileContentBytes, 0, fileContentBytes.Length);                            using (FileStream childFileStream = File.Create(fileFulleName))                            {                                childFileStream.Write(fileContentBytes, 0, fileContentBytes.Length);                            }                        }                    }                }            }        }    }}

調用樣本:

 List<string> strList = new List<string>() { @"D:\文檔\soapUI工程\Synchro-soapui-project.xml", @"D:\文檔\soapUI工程\PKBSML-soapui-project.xml", @"D:\文檔\soapUI工程\PKBSML-soapui-project.xml" };            var zipHelper = new Test.Zip.CompressHelper();            zipHelper.CompressMulti(strList.ToArray(), @"D:\wulala.gz");            zipHelper.DeCompressMulti(@"D:\wulala.gz", @"D:\web\");


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.