C# ZIP文件的压缩和解压缩(SharpZipLib.dll)

C# ZIP文件的压缩和解压缩(SharpZipLib.dll)真是折腾呀,网上虽然有不少的源码但测试几个就是不成功,经过折腾还是折腾出来了现在分享出来给大家。源码还是在网友们的基础上调整的,主要是调整源码大大小写格式。sharpziplib.dll下载:http://pan.baidu.com/share/link?shareid=1016448925&uk=134565274&fid=3214033513首先需要在项目里引用sharp

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

真是折腾呀,网上虽然有不少的源码但测试几个就是不成功,经过折腾还是折腾出来了现在分享出来给大家。

源码还是在网友们的基础上调整的,主要是调整源码大大小写格式。

sharpziplib.dll 下载:http://pan.baidu.com/share/link?shareid=1016448925&uk=134565274&fid=3214033513

首先需要在项目里引用sharpziplib.dll

ZipClass.cs 类函数 包括压缩和解压

using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
namespace Updatezip
{
    #region 压缩文件类
    /// <summary>
    /// 压缩文件   
    /// </summary>
    public class ZipClass
    {
        public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
        {
            //如果文件没有找到,则报错   
            if (!System.IO.File.Exists(FileToZip))
            {
                throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
            }
            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);
            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
            ZipEntry ZipEntry = new ZipEntry("ZippedFile");
            ZipStream.PutNextEntry(ZipEntry);
            ZipStream.SetLevel(CompressionLevel);
            byte[] buffer = new byte[BlockSize];
            System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
            ZipStream.Write(buffer, 0, size);
            try
            {
                while (size < StreamToZip.Length)
                {
                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                    ZipStream.Write(buffer, 0, sizeRead);
                    size += sizeRead;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            ZipStream.Finish();
            ZipStream.Close();
            StreamToZip.Close();
        }
        public void ZipFileMain(string[] args)
        {
            string[] filenames = Directory.GetFiles(args[0]);
            Crc32 Crc = new Crc32();
            ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
            s.SetLevel(6); // 0 - store only to 9 - means best compression   
            foreach (string file in filenames)
            {
                //打开压缩文件   
                FileStream fs = File.OpenRead(file);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(file);
                entry.DateTime = DateTime.Now;
                // set Size and the crc, because the information   
                // about the size and crc should be stored in the header   
                // if it is not set it is automatically written in the footer.   
                // (in this case size == crc == -1 in the header)   
                // Some ZIP programs have problems with zip files that don"t store   
                // the size and crc in the header.   
                entry.Size = fs.Length;
                fs.Close();
                Crc.Reset();
                Crc.Update(buffer);
 
                entry.Crc = Crc.Value;
                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
            s.Finish();
            s.Close();
        }
    }
#endregion
    #region 解压文件类
    /// <summary>
    ///  解压文件
    /// </summary>
    public class UnZipClass
    {
        public void UnZip(string[] args)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(args[1]);
                string fileName = Path.GetFileName(theEntry.Name);
                //生成解压目录   
                Directory.CreateDirectory(directoryName);
                if (fileName != String.Empty)
                {
                    //解压文件到指定的目录   
                    FileStream streamWriter = File.Create(args[1] + theEntry.Name);
                    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();
        }
    }
#endregion
}

调用方法

/// <summary>   
/// 调用源码   
/// </summary>   
private void button2_Click_1(object sender, System.EventArgs e)  
{  
    string[] FileProperties = new string[2];  
    FileProperties[0] = "C:\\unzipped\\";//待压缩文件目录   
    FileProperties[1] = "C:\\zip\\a.zip"; //压缩后的目标文件   
    ZipClass Zc = new ZipClass();  
    Zc.ZipFileMain(FileProperties);  
}  
private void button2_Click(object sender, System.EventArgs e)  
{  
    string[] FileProperties = new string[2];  
    FileProperties[0] = "C:\\zip\\test.zip";//待解压的文件   
    FileProperties[1] = "C:\\unzipped\\";//解压后放置的目标目录   
    UnZipClass UnZc = new UnZipClass();  
    UnZc.UnZip(FileProperties);  
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159431.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Cortex m33_STM32F4

    Cortex m33_STM32F4Cortex-M3Bit-Banding1.概述CM3的存储器系统支持所谓的“位带”(bit-band)操作。通过它,实现了对单一bit的原子操作。位带操作仅适用于一些特殊的存储器区域中。从汇编角度看:与传统方法的比较:在位带区中,每个比特都映射到别名地址区的一个字——这是个只有LSB才有效的字。支持位带操作的两个内存区的范围是:**0x2000_0000-0x

    2022年8月31日
    1
  • springmvc实现拦截器两种方式_追逐拦截他人的定义

    springmvc实现拦截器两种方式_追逐拦截他人的定义SpringMVC拦截器1、什么是拦截器  在系统中,经常需要在处理用户请求之前和之后执行一些行为,例如检测用户的权限,或者将请求的信息记录到日志中,即平时所说的“权限检测”及“日志记录”。当然不仅仅这些,所以需要一种机制,拦截用户的请求,在请求的前后添加处理逻辑。  SpringMVC的拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(InterceptorChain)。在访问被拦截的方法或字段时

    2025年8月11日
    2
  • tomcat各版本的区别

    tomcat各版本的区别tomcat各版本的区别

    2022年4月22日
    119
  • 系统环境变量与用户环境变量区别_windows7建立用户变量

    系统环境变量与用户环境变量区别_windows7建立用户变量区别:  环境变量分为系统环境变量和用户环境变量。环境变量是指系统环境变量,对所有用户起作用,而用户环境变量只对当前用户起作用。  例如你要用opencv,那么你把opencv的bin目录加入到path变量下面,那么它就是系统环境变量,所用用户登陆,在命令行输入opencv都会有opencv的帮助信息出来。而如果你在某个用户的变量下面新建一个变量,那么它就只对这个用户有用,当你以其他用户登陆时这

    2025年12月8日
    3
  • pycharm中html怎么运行_pycharm如何调试代码

    pycharm中html怎么运行_pycharm如何调试代码PyCharm调试程序,cmd中输入的变量怎么设置PyCharm调试程序,cmd中输入的变量的设置方法CMDdos定义变量,DOS下也只有环境变量可以用;SET[variable=[string]]variable指定环境变量名。string指定要指派给变量的一系列字符串。要显示当前环境变量,键入不带参数的SET。使用pycharm添加py文件,怎么调试PyCharm安装1…

    2022年8月25日
    8
  • java手机编程软件_手机java编程软件下载[通俗易懂]

    java手机编程软件_手机java编程软件下载[通俗易懂]手机java编程软件安卓版是一款专为java开发人员服务的编辑客户端应用,利用手机java编程软件手机安卓版实时进行相关编辑还能进行简单的编译,运行单个小程序等,提供您的效率。功能介绍手机java编程软件安卓版是一款将openjdk中关于编译java工程的代码移植到了安卓平台。手机java编程软件手机安卓版支持添加jar格式的lib文件,并且将编译后的程序dex化,以便在安卓设备上运行。在编译和运…

    2022年7月21日
    16

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号