最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)多文件壓縮與解壓功能

 更新時(shí)間:2022年08月30日 16:11:24   作者:Csharp 小記  
這篇文章主要為大家詳細(xì)介紹了如何利用C#語言實(shí)現(xiàn)多文件壓縮與解壓功能,即選擇多個(gè)文件壓縮成ZIP文件和解壓ZIP文件,需要的可以參考一下

這個(gè)功能沒什么可介紹的,大家都懂,直接上代碼了。。

實(shí)現(xiàn)功能

選擇多個(gè)文件壓縮成ZIP文件和解壓ZIP文件

開發(fā)環(huán)境

開發(fā)工具: Visual Studio 2013

.NET Framework版本:4.5

實(shí)現(xiàn)代碼

//需要添加ICSharpCode.SharpZipLib.Zip.dll到自己項(xiàng)目
 
 
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("請(qǐng)先選擇需要壓縮的文件");
         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("請(qǐng)先選擇解壓路徑");
         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 };
     }
 }

實(shí)現(xiàn)效果

到此這篇關(guān)于C#實(shí)現(xiàn)多文件壓縮與解壓功能的文章就介紹到這了,更多相關(guān)C#文件壓縮 解壓內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

临海市| 连州市| 荣成市| 瓮安县| 阿瓦提县| 陆丰市| 星子县| 诏安县| 信宜市| 天津市| 正宁县| 舒兰市| 盱眙县| 滕州市| 黄陵县| 法库县| 新丰县| 寻甸| 叙永县| 贡觉县| 高雄市| 马鞍山市| 长武县| 黔西| 乌拉特后旗| 日土县| 屯留县| 土默特左旗| 通河县| 潜山县| 定远县| 托里县| 墨脱县| 固镇县| 黎城县| 米泉市| 麻城市| 洪雅县| 八宿县| 海门市| 会理县|