C#实现zip压缩解压的程序代码

解压类

 代码如下 复制代码

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

namespace 安装界面
{

    public class UnZipClass
    {
        /// <summary>
        /// 解压功能(解压压缩文件到指定目录)
        /// </summary>
        /// <param name="FileToUpZip">待解压的文件</param>
        /// <param name="ZipedFolder">指定解压目标目录</param>
        public static void UnZip(string FileToUpZip, string ZipedFolder,string Password)
        {
            if (!File.Exists(FileToUpZip))
            {
                return;
            }

            if (!Directory.Exists(ZipedFolder))
            {
                Directory.CreateDirectory(ZipedFolder);
            }

            ZipInputStream s = null;
            ZipEntry theEntry = null;

            string fileName;
            FileStream streamWriter = null;
            try
            {
                s = new ZipInputStream(File.OpenRead(FileToUpZip));
                s.Password = Password;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    if (theEntry.Name != String.Empty)
                    {
                        fileName = Path.Combine(ZipedFolder, theEntry.Name);
                        ///判断文件路径是否是文件夹www.111cn.net
                        if (fileName.EndsWith("/") || fileName.EndsWith("//"))
                        {
                            Directory.CreateDirectory(fileName);
                            continue;
                        }
                        Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                        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;
                            }
                        }
                    }
                }
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                    streamWriter = null;
                }
                if (theEntry != null)
                {
                    theEntry = null;
                }
                if (s != null)
                {
                    s.Close();
                    s = null;
                }
                GC.Collect();
                GC.Collect(1);
            }
        }
    }
}

压缩类

 代码如下 复制代码

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

namespace 安装界面
{
    public class ZipClass
      {
          /// <summary>
          /// 递归压缩文件夹方法
          /// </summary>
          /// <param name="FolderToZip"></param>
          /// <param name="s"></param>
          /// <param name="ParentFolderName"></param>
          private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
          {
              bool res = true;
              string[] folders, filenames;
              ZipEntry entry = null;
              FileStream fs = null;
              Crc32 crc = new Crc32();
 
              try
              {
 
                  //创建当前文件夹
                  entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));  //加上 “/” 才会当成是文件夹创建
                  s.PutNextEntry(entry);
                  s.Flush();
 
 
                  //先压缩文件,再递归压缩文件夹
                  filenames = Directory.GetFiles(FolderToZip);
                  foreach (string file in filenames)
                  {
                      //打开压缩文件
                      fs = File.OpenRead(file);
 
                      byte[] buffer = new byte[fs.Length];
                      fs.Read(buffer, 0, buffer.Length);
                      entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
 
                      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);
                  }
              }
              catch
              {
                  res = false;
              }
              finally
              {
                  if (fs != null)
                  {
                      fs.Close();
                      fs = null;
                  }
                  if (entry != null)
                  {
                      entry = null;
                  }
                  GC.Collect();
                  GC.Collect(1);
              }
 
 
              folders = Directory.GetDirectories(FolderToZip);
              foreach (string folder in folders)
              {
                  if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
                  {
                      return false;
                  }
              }
 
              return res;
         }
 
         /// <summary>
         /// 压缩目录
         /// </summary>
         /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
         /// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
         /// <returns></returns>
         private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
         {
             bool res;
             if (!Directory.Exists(FolderToZip))
             {
                 return false;
             }
 
             ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
             s.SetLevel(6);
             s.Password = Password;
 
             res = ZipFileDictory(FolderToZip, s, "");
 
             s.Finish();
             s.Close();
 
             return res;
         }
 
         /// <summary>
         /// 压缩文件
         /// </summary>
         /// <param name="FileToZip">要进行压缩的文件名</param>
         /// <param name="ZipedFile">压缩后生成的压缩文件名</param>
         /// <returns></returns>
         private static bool ZipFile(string FileToZip, string ZipedFile, String Password)
         { //www.111Cn.net
             //如果文件没有找到,则报错
             if (!File.Exists(FileToZip))
             {
                 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
             }
             //FileStream fs = null;
             FileStream ZipFile = null;
             ZipOutputStream ZipStream = null;
             ZipEntry ZipEntry = null;
 
             bool res = true;
             try
             {
                 ZipFile = File.OpenRead(FileToZip);
                 byte[] buffer = new byte[ZipFile.Length];
                 ZipFile.Read(buffer, 0, buffer.Length);
                 ZipFile.Close();
 
                 ZipFile = File.Create(ZipedFile);
                 ZipStream = new ZipOutputStream(ZipFile);
                 ZipStream.Password = Password;
                 ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
                 ZipStream.PutNextEntry(ZipEntry);
                 ZipStream.SetLevel(6);
 
                 ZipStream.Write(buffer, 0, buffer.Length);
             }
             catch
             {
                 res = false;
             }
             finally
             {
                 if (ZipEntry != null)
                 {
                     ZipEntry = null;
                 }
                 if (ZipStream != null)
                 {
                     ZipStream.Finish();
                     ZipStream.Close();
                 }
                 if (ZipFile != null)
                 {
                     ZipFile.Close();
                     ZipFile = null;
                 }
                 GC.Collect();
                 GC.Collect(1);
             }
 
             return res;
         }
 
         /// <summary>
         /// 压缩文件 和 文件夹
         /// </summary>
         /// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
         /// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
         /// <returns></returns>
         public static bool Zip(String FileToZip, String ZipedFile, String Password)
         {
             if (Directory.Exists(FileToZip))
             {
                 return ZipFileDictory(FileToZip, ZipedFile, Password);
             }
            else if (File.Exists(FileToZip))
             {
                 return ZipFile(FileToZip, ZipedFile, Password);
             }
             else
             {
                 return false;
             }
         }
     }

}

时间: 2024-10-25 18:43:18

C#实现zip压缩解压的程序代码的相关文章

Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)

asp.net|示例|下载|压缩 (一). 实现功能    对文件及目录的压缩及解压功能(二). 运行图片示例  (三).代码    1. 压缩类     1/**//// <summary>  2/// 压缩类  3/// </summary>  4public class ZipClass  5{     6    public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLeve

在Java中操作Zip文件,压缩/解压

压缩 可随意转载,但请注明出处及作者SonyMusic2003.05.28==========================================================================在Java中操作Zip文件,压缩/解压 package test.nothing; import java.io.*;import java.util.*;import java.util.zip.*; import com.beaconsystem.util.*; impor

C#制作CAB压缩包压缩解压类

代码如下: 以下为引用的内容: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Diagnostics; namespace Com.ImYan.CabHelper { /// <summary> /// CAB文件压缩解压类 /// </summary> public class Cab { #region 属性列表 Propertie

Mac上zip文件解压出cpgz格式的文件该怎么办?

  Mac上zip文件解压出cpgz格式的文件该怎么办? 问题原因: 首先,我们分析导致这种情况的原因有以下几点: 1.zip文件已经损坏; 2.zip文件下载时没有下载完全; 3.浏览器在下载或者下载完成zip文件时,对其进行了错误处理; 4.bug导致. 校验文件: 解决这个zip-cpgz循环有多种方法,首先你可以验证一下你的zip文件md5 hash或者SHA1(这些数据一般可以在你下载的网页上边扎到),如果校验显示不同,说明你的文件有损坏或者下载不完全. 1.校验MD5 hash方法:

linux zip压缩和解压的各种操控

1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录 2.把/home目录下面的mydata.zip解压到mydatabak目录里面 unzip mydata.zip -d mydatabak 3.把/home目录下面的abc文件夹和123.txt压缩成为abc123.zip zip -r abc123.zip abc 123.txt 4.把/home目录下面的wwwroot.zip直接解压到/home目录

Centos学习笔记 linux 常用命令:压缩解压命令

  2012-2-9 星期4 linux 常用命令:压缩解压命令 -gz命令的英文原意:Gnu zip 语法:gzip 选项[文件] 功能描述:压缩文件 压缩后文件格式: .gz 1. 只能压缩文件,不能压缩目录 2.不保留源文件 解压缩命令:gunzip 语法:gunzip选项[压缩文件] 功能描述:解压缩.gz的压缩文件 范例:gunzip file1.gz 压缩解压目录:tar 命令名称:tar 语法 tar选项[cvf][目录]      -c 产生.tar打包文件      -v 显示

打包-怎样写一个压缩解压Shell脚本

问题描述 怎样写一个压缩解压Shell脚本 文件的名字不能写死, 可以传入一个参数,打包指定的文件 求教各位大神怎么写 解决方案 #!/bin/sh tar zcvf $1.tar.gz $1 解决方案二: tar zcf foo.tgz foo 解决方案三: 主要看你想压缩成什么格式的. tar.gz, tar, tar.Z, zip, tar.z, etc. 传入两个参数,一个参数是压缩类型,一个是压缩的文件 下面是脚本里的一个压缩的写法. #!/bin/sh tar -zcvf $2.$1

C# 文件流压缩解压

/// <summary> /// 文件流压缩解压 /// </summary> public class ZipHelper { public static int BEST_COMPRESSION = 9; public static int BEST_SPEED = 1; public static int DEFAULT_COMPRESSION = -1; public static int NO_COMPRESSION = 0; #region Deflate压缩 #re

C# txt或html文件 关于在线压缩解压问题

问题描述 如何实现A服务器将文件(txt/html)压缩后传到B服务器上并解压还原保存文件,解压文件不能在本地生成.有没有可能实现?如果觉得我秒速得不太清楚.以下是原始需要解决的问题.1.蜘蛛扒到的网站太大,需要节省流量压缩后才上传到服务器.2.压缩解压文件会耗费硬盘资源和CPU.所以希望全由线上程序处理 解决方案 解决方案二:无法回答,你先解释什么是不能在"本地"生成,什么又是全由"线上"程序处理,服务器A采用何种方式把文件上传到服务器B,你所能修改的又是哪部分的