您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用C#實現多文件壓縮與解壓功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用C#實現多文件壓縮與解壓功能”吧!
選擇多個文件壓縮成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#實現多文件壓縮與解壓功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。