我是靠谱客的博主 爱听歌可乐,最近开发中收集的这篇文章主要介绍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
    }
}

最后

以上就是爱听歌可乐为你收集整理的C# 压缩,解压的全部内容,希望文章能够帮你解决C# 压缩,解压所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部