首頁 > 軟體

C#實現多檔案壓縮與解壓功能

2022-08-30 22:02:23

這個功能沒什麼可介紹的,大家都懂,直接上程式碼了。。

實現功能

選擇多個檔案壓縮成ZIP檔案和解壓ZIP檔案

開發環境

開發工具: Visual Studio 2013

.NET Framework版本:4.5

實現程式碼

//需要新增ICSharpCode.SharpZipLib.Zip.dll到自己專案
 
 
private void btnCompressFile_Click(object sender, EventArgs e)
 {
     listFiles.Items.Clear();
     OpenFileDialog ofd = new OpenFileDialog();
     ofd.Multiselect = true;
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         listFiles.Items.AddRange(ofd.FileNames);
 
     }
 }
 
 private void btnCompress_Click(object sender, EventArgs e)
 {
     if (listFiles.Items.Count == 0)
     {
         MessageBox.Show("請先選擇需要壓縮的檔案");
         return;
     }
     SaveFileDialog sfd = new SaveFileDialog();
     sfd.Filter = "壓縮檔案|*.zip";
     if (sfd.ShowDialog() == DialogResult.OK)
     {
         string[] files = new string[listFiles.Items.Count];
         for (int i = 0; i < listFiles.Items.Count; i++)
         {
             files[i] = listFiles.Items[i].ToString();
         }
         dynamic result;
         using (ZipOutputStream outStream = new ZipOutputStream(File.Create(sfd.FileName)))
         {
             result = Zip(files, outStream, "123");
         }
         MessageBox.Show(result.msg);
 
     }
 
 }
 
 private void btnUnCompressFile_Click(object sender, EventArgs e)
 {
     FolderBrowserDialog fbd = new FolderBrowserDialog();
     fbd.ShowNewFolderButton = true;
     if (fbd.ShowDialog() == DialogResult.OK)
     {
         txtOutFile.Text = fbd.SelectedPath;
     }
 }
 
 private void btnUnCompress_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrWhiteSpace(txtOutFile.Text))
     {
         MessageBox.Show("請先選擇解壓路徑");
         return;
     }
     OpenFileDialog ofd = new OpenFileDialog();
     ofd.Filter = "壓縮檔案|*.zip";
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         dynamic result = UnZip(ofd.FileName, txtOutFile.Text,"123");
         MessageBox.Show(result.msg);
     }
 }
 public dynamic Zip(string[] files, ZipOutputStream outStream, string pwd)
 {
     try
     {
         for (int i = 0; i < files.Length; i++)
         {
             if (!File.Exists(files[i]))
             {
                 throw new Exception("檔案不存在");
             }
             using (FileStream fs = File.OpenRead(files[i]))
             {
                 byte[] buffer = new byte[fs.Length];
                 fs.Read(buffer, 0, buffer.Length);
                 if (!string.IsNullOrWhiteSpace(pwd))
                 {
                     outStream.Password = pwd;
                 }
                 ZipEntry ZipEntry = new ZipEntry(Path.GetFileName(files[i]));
                 outStream.PutNextEntry(ZipEntry);
                 outStream.Write(buffer, 0, buffer.Length);
             }
         }
 
         return new { result = true, msg = "壓縮成功" };
     }
     catch (Exception ex)
     {
         return new { result = true, msg = "壓縮失敗:" + ex.Message };
     }
 }
 
 public dynamic UnZip(string zipFile, string outPath, string pwd)
 {
     try
     {
         if (!Directory.Exists(outPath))
         {
             Directory.CreateDirectory(outPath);
         }
         using (ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(zipFile)))
         {
             if (!string.IsNullOrWhiteSpace(pwd))
             {
                 zipInputStream.Password = pwd;
             }
             ZipEntry theEntry;
             while ((theEntry = zipInputStream.GetNextEntry()) != null)
             {
                 using (FileStream streamWriter = File.Create(outPath + "\" + theEntry.Name))
                 {
                     byte[] data = new byte[1024 * 1024];
                     int dataLength = 0;
                     while ((dataLength = zipInputStream.Read(data, 0, data.Length)) > 0)
                     {
                         streamWriter.Write(data, 0, dataLength);
                     }
                 }
 
             }
         }
         return new { result = true, msg = "解壓成功" };
     }
     catch (Exception ex)
     {
         return new { result = true, msg = "解壓失敗:" + ex.Message };
     }
 }

實現效果

到此這篇關於C#實現多檔案壓縮與解壓功能的文章就介紹到這了,更多相關C#檔案壓縮 解壓內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com