C# 檔案的壓縮與解壓縮

來源:互聯網
上載者:User

需要引用 using ICSharpCode.SharpZipLib.Zip;

調用:CreateZipFile("檔案路徑","壓縮路徑")

 private static void CreateZipFile(string filesPath, string zipFilePath)
        {

            if (!Directory.Exists(filesPath))
            {
                MessageBox.Show("找不到 " + filesPath);
                return;
            }

            try
            {
                string[] filenames = Directory.GetFiles(filesPath);
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                {

                    s.SetLevel(9); // 壓縮層級 0-9
                    //s.Password = "123"; //Zip壓縮檔密碼
                    byte[] buffer = new byte[4096]; //緩衝區大小
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

 

調用 : UnZipFile("壓縮路徑","檔案路徑")

private static void UnZipFile(string zipFilePath, string unZipFilePath)
        {
            if (!File.Exists(zipFilePath))
            {
                MessageBox.Show(zipFilePath + " 不存在!");
                return;
            }

            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
           
            {

                ZipEntry theEntry;
                string directoryName = Path.GetDirectoryName(unZipFilePath);
                // create directory
                if (directoryName.Length > 0)
                {
                    Directory.CreateDirectory(directoryName);
                }

                while ((theEntry = s.GetNextEntry()) != null)
                {
                    //string fileName = Path.GetFileName(theEntry.Name);
                    string fileName = Path.Combine(unZipFilePath,theEntry.Name);

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(fileName))
                        {

                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                           
                            streamWriter.Close();
                        }
                    }
                }

                s.Close();
            }
        }

 

 

//-- 補充

C#使用ICSharpCode SharpZipLib 壓縮 解壓縮 可以壓縮檔夾 

解壓縮代碼:

using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace DRMEncryption
{
/// ;summary;
/// UnZip 類用於解壓縮一個 zip 檔案。
/// ;/summary;
public class UnZipDir
{

//解壓縮以後的檔案名稱和路徑,壓縮前的路徑
public static Boolean UNZipFile(string FileToZip, string ZipedFile)
{
try
{
FastZip fastZip = new FastZip();
fastZip.ExtractZip(FileToZip, ZipedFile, "");
return true;
}
catch {
return false;
}

}
}
}

壓縮檔代碼:

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace DRMEncryption
{
public class ZipClass
{

public static Boolean ZipFile(string FileToZip, string ZipedFile)
{

try
{
FastZip fastZip = new FastZip();

bool recurse = true;

//壓縮後的檔案名稱,壓縮目錄 ,是否遞迴

fastZip.CreateZip(FileToZip, ZipedFile, recurse, "");
return true;
}
catch { return false; }

}

}
}

那個返回方法是用來另外一個頁面判斷解壓是否成功用的,可以不要;

解壓縮:UnZipDir.UNZipFile(檔案名稱, 路徑);

壓縮:ZipClass.ZipFile(檔案名稱, 路徑);

http://hi.baidu.com/supplyair/blog/item/9f75072c55c498eb8a1399ad.html

解壓縮代碼:

using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace DRMEncryption
{
/// ;summary;
/// UnZip 類用於解壓縮一個 zip 檔案。
/// ;/summary;
public class UnZipDir
{

//解壓縮以後的檔案名稱和路徑,壓縮前的路徑
public static Boolean UNZipFile(string FileToZip, string ZipedFile)
{
try
{
FastZip fastZip = new FastZip();
fastZip.ExtractZip(FileToZip, ZipedFile, "");
return true;
}
catch {
return false;
}

}
}
}

壓縮檔代碼:

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace DRMEncryption
{
public class ZipClass
{

public static Boolean ZipFile(string FileToZip, string ZipedFile)
{

try
{
FastZip fastZip = new FastZip();

bool recurse = true;

//壓縮後的檔案名稱,壓縮目錄 ,是否遞迴

fastZip.CreateZip(FileToZip, ZipedFile, recurse, "");
return true;
}
catch { return false; }

}

}
}

那個返回方法是用來另外一個頁面判斷解壓是否成功用的,可以不要;

解壓縮:UnZipDir.UNZipFile(檔案名稱, 路徑);

壓縮:ZipClass.ZipFile(檔案名稱, 路徑);

http://hi.baidu.com/supplyair/blog/item/9f75072c55c498eb8a1399ad.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.