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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python聊天室(tkinter写界面,treading,socket实现私聊群聊查看聊天记录,mysql存储数据)

    python聊天室(tkinter写界面,treading,socket实现私聊群聊查看聊天记录,mysql存储数据)一、前言我用的是面向对象写的,把界面功能模块封装成类,然后在客户端创建对象然后进行调用。好处就是方便我们维护代码以及把相应的信息封装起来,每一个实例都是各不相同的。所有的界面按钮处理事件都在客户端,在创建界面对象是会把客户端的处理事件函数作为创建对象的参数,之后再按钮上绑定这个函数,当点击按钮时便会回调函数二、登录界面实现登录界面模块chat_login_panel.pyfromtkinterimport*#导入模块,用户创建GUI界面#登陆界面类classLoginPane

    2025年7月15日
    5
  • H2数据库相关介绍「建议收藏」

    H2数据库相关介绍「建议收藏」什么是H2数据库H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式,可以兼容一些主流的数据库,因此采用H2作为开发期的数据库非常方便。H2是纯java编写的,源码大小只有1M左右。优点:速度非常快,开源,JDBCAPI嵌入式和服务器模式;内存数据库基于浏览器的Console应用…

    2022年10月12日
    5
  • word中行与行间距大

    word中行与行间距大word中设置了行间距,但还是显示距离过大。如下图,删除段前间距和段后间距

    2022年6月13日
    55
  • DNS服务器的配置和管理

    DNS服务器的配置和管理在vmware中添加并打开windowserver2003 链接:https://pan.baidu.com/s/1M0AHFe8M3932SUIh3vLwWA密码:i3s7一、实验目的1.掌握Windows2000DNS服务器的安装和配置3.掌握DNS客户端的配置2.了解DNS的工作原理二、实验原理1.名称服务器   根据工作方式的不同,授权名称服务器可以分为:主名称服务器、辅助名称服…

    2022年5月28日
    36
  • C DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用

    C DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用1 nbsp nbsp nbsp nbsp nbsp DLLImport 的使用 nbsp usingSystem usingSystem Runtime InteropServi 命名空间 classExample 用 DllImport 导入 Win32 的 MessageBox 函数 nbsp nbsp nbsp DllImport user32 dll CharSet CharSet U

    2025年12月12日
    5
  • DCache 分布式存储系统|List 缓存模块的创建与使用[通俗易懂]

    DCache 分布式存储系统|List 缓存模块的创建与使用[通俗易懂]作者|Eaton导语|在之前的系列文章中,我们介绍了DCache及其KV和K-K-Row缓存模块的使用,本文将继续介绍如何使用DCache中的列表类型缓存模块——List缓存模块。系列文章DCache分布式存储系统|安装部署与应用创建DCache分布式存储系统|Key-Value缓存模块的创建与使用DCache分布式存储系统|K-K-Row缓存模块的创建与使用DCache分布式存储系统|List缓存模块的创建与使用目录List模块简介创建.

    2022年8月30日
    3

发表回复

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

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