C# 压缩,解压

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

namespace CommonFTP
{
    /// <summary>
    /*----------------------------------------------------------------
           * Copyright (C) 2008 Dealeasy
           *
           * File Function: 压缩文件和解压缩文件
           *
           * Version History
           * Date                Author                      Description
           * 08/03/28                Created
           *
    ----------------------------------------------------------------*/
    /// </summary>
    public class CommCompressFile
    {
        /// <summary>
        /// 压缩
        /// at 06/03 2008 updated by RoyHE
        /// Update CompressFile (for two or more files to commpress)
        /// </summary>
        /// <param name="pathList"></param>
        /// <param name="OutZipPath"></param>
        public void CompressFile(string[] pathList,string OutZipPath)
        {           
            new CommonFTPTransmission().CreateNotExistsFolder(OutZipPath);

            //Create output stream
            ZipOutputStream output = new ZipOutputStream(File.Create(OutZipPath));

            Crc32 crc = new Crc32();

            output.SetLevel(6);

            try
            {
                //Loop at PathList
                for (int i = 0; i < pathList.Length; i++)
                {
                    //打开压缩文件
                    FileStream fs = File.OpenRead(pathList[i]);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string tempfile = pathList[i].Substring(pathList[i].LastIndexOf("//") + 1);
                    ZipEntry entry = new ZipEntry(tempfile);
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    output.PutNextEntry(entry);
                    output.Write(buffer, 0, buffer.Length);
                }

                // Close the OutStream
                output.Finish();
                output.Close();
            }
            catch (Exception ex)
            {
                output.Finish();
                output.Close();
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 解压文件
        /// at 06/03 2008 updated by RoyHE
        /// Update DecompressFile (for two or more files to commpress)
        /// bol:is delete directory(是否删除已存在的解压文件夹)
        /// </summary>
        /// <param name="compressFileName"></param>
        /// <param name="bol"></param>
        public void DecompressFile(string compressFileName,bool bol)
        {
            //创建输入流
            ZipInputStream inputStream = new ZipInputStream(File.OpenRead(compressFileName));
            ZipEntry entry;
            string strTargetFile = string.Empty;

            //删除目录
            string strDir = compressFileName.Remove(compressFileName.LastIndexOf("."));
            //目录中存在,执行
            if (System.IO.Directory.Exists(strDir))
            {
                if (bol)
                {
                    System.IO.Directory.Delete(strDir, bol);
                    System.IO.Directory.CreateDirectory(strDir);
                }
            }
            //目录不存在,创建目录
            else System.IO.Directory.CreateDirectory(strDir);

            try
            {
                while ((entry = inputStream.GetNextEntry()) != null)
                {

                    strTargetFile = strDir + "//" + entry.Name;
                    int currentSize = 0;
                    byte[] buffer = new byte[4096];
                    FileStream output = File.Create(strTargetFile);
                    while (true)
                    {
                        int bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead > 0)
                        {
                            output.Write(buffer, 0, bytesRead);
                            currentSize += bytesRead;
                        }
                        else
                        {
                            output.Flush();
                            break;
                        }
                    }
                    output.Close();
                    File.SetLastWriteTime(strTargetFile, entry.DateTime);
                }
                
                inputStream.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #region
        / <summary>
        / 解压
        / </summary>
        / <param name="entry"></param>
        //private void ExtractFileEntry(ZipEntry entry, string workDirectory, ZipInputStream inputStream)
        //{
        //    string targetName = entry.Name;
        //    int currentSize = 0;
        //    byte[] buffer = new byte[4096];
        //    try
        //    {
        //        FileStream output = File.Create(@"d:/data.xls");
        //        while (true)
        //        {
        //            int bytesRead = inputStream.Read(buffer, 0, buffer.Length);
        //            if (bytesRead > 0)
        //            {
        //                output.Write(buffer, 0, bytesRead);
        //                currentSize += bytesRead;
        //            }
        //            else
        //            {
        //                output.Flush();
        //                break;
        //            }
        //        }
        //        output.Close();
        //        File.SetLastWriteTime(targetName, entry.DateTime);

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {

        //    }
        //}

        
        //public void ZipFile(string strFile, string strZip)
        //{
        //    strZip = @"d:/testRoy.zip";
        //    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
        //        strFile += Path.DirectorySeparatorChar;
        //    ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
        //    s.SetLevel(6); // 0 - store only to 9 - means best compression
        //    zip(strFile, s, strZip);
        //    s.Finish();
        //    s.Close();
        //}
        //private void zip(string strFile, ZipOutputStream s, string StoreFile)
        //{
        //    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
        //        strFile += Path.DirectorySeparatorChar;
        //    Crc32 crc = new Crc32();
        //    //string[] filenames = Directory.GetFileSystemEntries(strFile);
        //    string[] filenames = new string[2];
        //    filenames[0] = @"d:/data.xls";
        //    filenames[1] = @"d:/sql.xls";
        //    foreach (string file in filenames)
        //    {
        //        if (Directory.Exists(file))
        //        {
        //            zip(file, s, StoreFile);
        //        }
        //        else
        //        {
        //            //打开压缩文件
        //            FileStream fs = File.OpenRead(file);
        //            byte[] buffer = new byte[fs.Length];
        //            fs.Read(buffer, 0, buffer.Length);
        //            string tempfile = file.Substring(StoreFile.LastIndexOf("//") + 1);
        //            ZipEntry entry = new ZipEntry(tempfile);
        //            entry.DateTime = DateTime.Now;
        //            entry.Size = fs.Length;
        //            fs.Close();
        //            crc.Reset();
        //            crc.Update(buffer);
        //            entry.Crc = crc.Value;
        //            s.PutNextEntry(entry);
        //            s.Write(buffer, 0, buffer.Length);
        //        }
        //    }
        //}
        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值