ICSharpCode.SharpZipLib 使用演示

using System;
using System.Data;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;

namespace Test.CUI
{
  class Zip
  {
    static void CompressFile()
    {
      FileStream ins = File.OpenRead("1.jpg");
      FileStream outs = File.Create("test.zip");
      
      ZipOutputStream s = new ZipOutputStream(outs);
      s.SetLevel(5);
      s.Password = "123456";

      ZipEntry entry = new ZipEntry("1.jpg");
      s.PutNextEntry(entry);

      byte[] buffer = new byte[ins.Length];
      ins.Read(buffer, 0, buffer.Length);
      s.Write(buffer, 0, buffer.Length);

      s.Finish();
      s.Close();
    }

    static MemoryStream UnCompressToMemory()
    {
      ZipInputStream s = new ZipInputStream(File.OpenRead("test.zip"));
      s.Password = "123456";
      
      ZipEntry theEntry = s.GetNextEntry();
      Console.WriteLine(theEntry.Name);

      MemoryStream ms = new MemoryStream((int)theEntry.Size);
      byte[] data = new byte[1024 * 100];
 
      while (true)
      {
        int size = s.Read(data, 0, data.Length);

        if (size > 0)
        {
          ms.Write(data, 0, size);
        }
        else
        {
          break;
        }
      }

      Console.WriteLine(ms.Length);
      s.Close();

      return ms;
    }

    static void UpCompressToFile()
    {
      
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VS2015中导入icsharpcode.sharpziplib可以按照以下步骤进行操作: 1. 首先,从icsharpcode.sharpziplib的官方网站(https://github.com/icsharpcode/SharpZipLib)下载最新的库文件。 2. 打开VS2015,创建一个新的项目或打开现有的项目。 3. 在VS2015的解决方案资源管理器中,右键单击项目名称,然后选择“管理NuGet程序包”。 4. 在NuGet程序包管理器中,点击“浏览”选项卡,并在搜索框中输入“SharpZipLib”进行搜索。 5. 在搜索结果中找到“SharpZipLib”并点击“安装”按钮。 6. NuGet程序包管理器将自动下载和安装icsharpcode.sharpziplib。 7. 安装完成后,你可以在代码中使用icsharpcode.sharpziplib的类和方法。 例如,在文件中使用SharpZipLib压缩文件: ```csharp using System; using ICSharpCode.SharpZipLib.Zip; class Program { static void Main() { string sourceFile = @"C:\source\file.txt"; string zipFile = @"C:\destination\archive.zip"; using (ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipFile))) { zipStream.SetLevel(9); //设置压缩级别(0-9) byte[] buffer = new byte[4096]; ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(sourceFile)); zipStream.PutNextEntry(entry); using (System.IO.FileStream fileStream = System.IO.File.OpenRead(sourceFile)) { int sourceBytes; do { sourceBytes = fileStream.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } Console.WriteLine("文件已成功压缩!"); Console.ReadLine(); } } ``` 通过以上步骤,你就可以成功导入并使用icsharpcode.sharpziplib库在VS2015中进行文件压缩和解压缩操作了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值