C#实现文件压缩解压类

 C#实现文件压缩解压类          

        首先需要在项目里引用ICSharpCode.SharpZipLib.dll。

 下载网址:http://ncu.dl.sourceforge.net/project/sharpdevelop/SharpZipLib/0.86/SharpZipLib_0860_Bin.zip 

注:本类引用的是 0.86 版 

压缩 与 解压类如下 (注:仅支持 .zip 的压缩与解压)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using System.Text;

namespace 文件压缩与解压
{
    public class ZipClass
    {

        #region 压缩文件
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileToZip">待压缩的文件</param>
        /// <param name="ZipedFile">压缩后的文件</param>
        /// <param name="password">压缩密码</param>
        private void ZipFile(string FileToZip, string ZipedFile,string password)
        {   //如果文件没有找到,则报错 
            if (!System.IO.File.Exists(FileToZip))
            {
                throw new System.IO.FileNotFoundException("文件 " + FileToZip + "不存在!");
            }
            //取得待压缩文件流
            System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //创建压缩后的文件
            System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
            //创建新的ZIP输出流
            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
            // 每一个被压缩的文件都用ZipEntry表示,需要为每一个压缩后的文件设置名称  
            ZipEntry ZipEntry = new ZipEntry("ZippedFile");
            //设置每一个ZipEntry对象
            ZipStream.PutNextEntry(ZipEntry);
            // 为后续的 DEFLATED 条目设置压缩级别。 0 -9
            ZipStream.SetLevel(6);
            //设置解压密码
            ZipStream.Password = password;
            //每次写入1024个字节
            byte[] buffer = new byte[1024];
            int size = 0; //已写入压缩流的字节数
            try
            {
                //如果没有写入完成
                while (size < StreamToZip.Length)
                {
                    //将文件内容写入buffer
                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                    //将字节写入压缩流
                    ZipStream.Write(buffer, 0, sizeRead);
                    size += sizeRead;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            //完成写入 ZIP 输出流的内容,无需关闭底层流。
            ZipStream.Finish();
            //关闭 ZIP 输出流和正在过滤的流。
            ZipStream.Close();
            //关闭文件流
            StreamToZip.Close();
        }
        #endregion 压缩文件

        #region 压缩文件夹(私有不被外部调用)
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="FileToZip">待压缩的文件夹</param>
        /// <param name="ZipStream">压缩文件流</param>
        /// <param name="ParentFolderName">压缩父目录</param>
        private void ZipFolder(string FolderToZip, ZipOutputStream ZipStream, string ParentFolderName)
        {   //如果文件夹没有找到,则报错 
            if (!System.IO.Directory.Exists(FolderToZip))
            {
                throw new System.IO.FileNotFoundException("文件路径 " + FolderToZip + "不存在!");
            }
            //校验类
            Crc32 crc = new Crc32();
            //取得路径下所有文件全路径
            string[] filenames = Directory.GetFiles(FolderToZip);
           
            //循环压缩文件
            foreach (string file in filenames)
            {    //打开要的压缩文件 
                if (!System.IO.File.Exists(file)) //判断文件是否存在
                {
                    throw new System.IO.FileNotFoundException("待压缩的文件 " + file + "不存在!");
                }
                //取得待压缩文件流
                FileStream fs = File.OpenRead(file);
                byte[] buffer = new byte[fs.Length];
                //将文件写入字节数组
                fs.Read(buffer, 0, buffer.Length);

                //压缩文件后的目录
                string ZipPath = Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file));
                ZipEntry entry = new ZipEntry(ZipPath); 
                entry.DateTime = DateTime.Now;
                //设置size
                entry.Size = fs.Length;
                //关闭文件流
                fs.Close();
                // 将 CRC-32 重置为初始值。
                crc.Reset();
                // 使用指定的字节数组更新校验和。
                crc.Update(buffer);
                // 设置 CRC-32 值。
                entry.Crc = crc.Value;
                //设置每一个ZipEntry对象
                ZipStream.PutNextEntry(entry);
                //将字节写入压缩流
                ZipStream.Write(buffer, 0, buffer.Length);
            }
           
            //取得路径下所有文件夹全路径
            string[] folders = Directory.GetDirectories(FolderToZip);
            //循环压缩文件夹下层目录的文件
            foreach (string folder in folders)
            {
                ZipFolder(folder, ZipStream, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip)));
            }
        }
        #endregion 压缩文件夹

        #region 压缩文件夹 (可被外部应用)
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="FileToZip">待压缩的文件夹</param>
        /// <param name="ZipStream">压缩后的文件</param>
        /// <param name="password">压缩密码</param>
        public void ZipFolder(string FolderToZip, string ZipedFile, string password)
        {
           // 解决中文乱码
            Encoding gbk = Encoding.GetEncoding("gbk");
            ZipConstants.DefaultCodePage = gbk.CodePage;

            //创建压缩后的文件
            System.IO.FileStream zipFile = System.IO.File.Create(ZipedFile);
            //创建新的ZIP输出流
            ZipOutputStream ZipStream = new ZipOutputStream(zipFile);
            //设置解压密码
            ZipStream.Password = password;
            // 为后续的 DEFLATED 条目设置压缩级别。 0 -9
            ZipStream.SetLevel(6);
            //压缩文件夹
            ZipFolder(FolderToZip, ZipStream, "");
            //完成写入 ZIP 输出流的内容,无需关闭底层流。
            ZipStream.Finish();
            //关闭 ZIP 输出流和正在过滤的流。
            ZipStream.Close();
        }
        #endregion 压缩文件夹

        #region 压缩文件(根据路径 自动判定是文件夹还是文件)
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileToZip">待压缩的文件(夹)路径</param>
        /// <param name="ZipStream">压缩后的文件</param>
        /// <param name="password">压缩密码</param>
        public void ZipFileMain(string FileToZip, string ZipedFile, string password)
        {
            //为压缩文件夹
            if (Directory.Exists(FileToZip))
            {
                //压缩文件夹
                ZipFolder(FileToZip, ZipedFile, password);
            }
            //为压缩文件
            else if (File.Exists(FileToZip))
            {
                // //压缩文件
                ZipFile(FileToZip, ZipedFile, password);
            }
            else {
                throw new System.IO.FileNotFoundException("待压缩的文件目录 " + FileToZip + "出错!");
            }
        }
           #endregion 压缩文件(根据路径 自动判定是文件夹还是文件)


        #region 解压文件
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="args">
        //    args[0] = Server.MapPath("ZIP") + "\\f12Zip.zip"; //待解压的文件  
        //   args[1]=Server.MapPath("UPZIP\\");//解压后放置的目标目录 
        //  或
        //    args[0] =  "D:\\f12Zip.zip"; //待解压的文件  
        //   args[1]="D:\\UPZIP\\");  //解压后放置的目标目录 
        //</param>
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="UpZipFile">待解压文件</param>
        /// <param name="ZipToFile">解压后放置的目标目录</param>
        /// <param name="password">解压密码</param>
        public void UnZip(string UpZipFile, string ZipToFile,string password)
        {
            if (!System.IO.File.Exists(UpZipFile))
            {
                throw new System.IO.FileNotFoundException("文件 " + UpZipFile + "不存在!");
            }
            //创建新的ZIP输入流
            ZipInputStream ZipStream = new ZipInputStream(File.OpenRead(UpZipFile));
            //设置解压密码
            ZipStream.Password = password;
            ZipEntry theEntry;
            while ((theEntry = ZipStream.GetNextEntry()) != null)  
            {
                //取得解压后的目录
                string directoryName = Path.GetDirectoryName(ZipToFile);
                //取得解压文件下的文件名 
                string fileName = Path.GetFileName(theEntry.Name);
                //取得子目录
                string filepath = Path.GetDirectoryName(theEntry.Name);
                //取得解压文件名
                //如 xxxxx.zip
                string ZipFile = Path.GetFileName(UpZipFile);
                //去掉文件名后缀 xxxxx
                string zipfile = ZipFile.Split('.')[0];
                //创建解压后文件目录
                string filePath = directoryName + "\\" + zipfile + "\\" + filepath + "\\";
                Directory.CreateDirectory(filePath);
                if (fileName != String.Empty)
                {        
                    //解压文件到指定的目录    
                    FileStream streamWriter = File.Create(filePath + "\\" + fileName);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = ZipStream.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else { break; }
                    }
                    streamWriter.Close();
                }
            }
            ZipStream.Close();
        }
        #endregion 解压文件

    }
}


 

调用方式:

             string filepath = Server.MapPath("freezip");
            string strtxtPath = filepath;  //压缩文件夹
            string strzipPath = Server.MapPath("ZIP") + "
\\f12Zip.zip";

            //  strtxtPath= "C:\\unzipped\\";//待压缩文件目录 
            //  strzipPath="C:\\zip\\a.zip";  //压缩后的目标文件  
                 ZipClass zc = new ZipClass();
            zc.ZipFileMain(strtxtPath,strzipPath,""); //压缩文件

解压相似。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现文件压缩可以使用Java内置的ZipOutputStream。下面是一个示例代码: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileCompressor { public static void main(String[] args) throws Exception { String sourceFile = "source.txt"; String compressedFile = "compressed.zip"; compress(sourceFile, compressedFile); } private static void compress(String sourceFile, String compressedFile) throws Exception { FileOutputStream fos = new FileOutputStream(compressedFile); ZipOutputStream zos = new ZipOutputStream(fos); File file = new File(sourceFile); FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); zos.close(); fos.close(); } } ``` C#实现文件压缩可以使用System.IO.Compression.ZipArchive。下面是一个示例代码: ```csharp using System.IO; using System.IO.Compression; class Program { static void Main(string[] args) { string sourceFile = "source.txt"; string compressedFile = "compressed.zip"; Compress(sourceFile, compressedFile); } private static void Compress(string sourceFile, string compressedFile) { using (FileStream fs = new FileStream(compressedFile, FileMode.Create)) { using (ZipArchive zipArchive = new ZipArchive(fs, ZipArchiveMode.Create)) { zipArchive.CreateEntryFromFile(sourceFile, Path.GetFileName(sourceFile)); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值