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()
    {
      
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章