zoukankan      html  css  js  c++  java
  • The Zip, GZip, BZip2 and Tar Implementation For .NET

    #ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented
    as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language). The creator of #ziplib put it this
    way: "I've ported the zip library over to C# because I needed gzip/zip compression and I didn't want to use libzip.dll or something like this. I want
    all in pure C#."

    Ref:http://www.icsharpcode.net/

    Ref:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

    Example:

    Reference:ICSharpCode.SharpZipLib.dll
    ----------------------------------------------
    using System.IO;
    using ICSharpCode.SharpZipLib.Core;
    using ICSharpCode.SharpZipLib.Zip;
    
    string sourceDir = "d:\ziptest";
    string targetZipDir = "d:\ziptest\zip";
    string targetUnZipDir = "d:\ziptest\unzip";
    string targetZipFile = "d:\ziptest\zip\temp";
    Init
                var astrFileNames = Directory.GetFiles(sourceDir);
    
                if (!Directory.Exists(targetZipDir))
                {
                    Directory.CreateDirectory(targetZipDir);
                }
    
                var strmZipOutputStream = new ZipOutputStream(File.Create(targetZipFile));
    
                try
                {
                    strmZipOutputStream.SetLevel(9);
                    byte[] abyBuffer = new byte[4096];
    
                    foreach (var strFile in astrFileNames)
                    {
                        var strmFile = File.OpenRead(strFile);
                        try
                        {
                            var objZipEntry = new ZipEntry(strFile);
    
                            objZipEntry.DateTime = DateTime.Now;
                            objZipEntry.Size = strmFile.Length;
    
                            strmZipOutputStream.PutNextEntry(objZipEntry);
                            StreamUtils.Copy(strmFile, strmZipOutputStream, abyBuffer);
                        }
                        finally
                        {
                            strmFile.Close();
                        }
    
    
                    }
    
                    strmZipOutputStream.Finish();
                }
                finally
                {
                    strmZipOutputStream.Close();
                }
    Zip Code
                try
                {
                    using (ZipInputStream s = new ZipInputStream(File.OpenRead(targetZipFile)))
                    {
    
                        ZipEntry theEntry;
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string fileName = Path.GetFileName(theEntry.Name);
    
                            if (!Directory.Exists(targetUnZipDir))
                            {
                                Directory.CreateDirectory(targetUnZipDir);
                            }
    
                            if (fileName != String.Empty)
                            {
                                using (FileStream streamWriter = File.Create(targetUnZipDir + "\" + Path.GetFileName(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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    UnZip Code
  • 相关阅读:
    scrapy 中间件
    索引
    理解平均负载
    jquery1
    网络编程
    模块
    进程
    图书管理系统用ajax删除书籍
    mysql
    jquery
  • 原文地址:https://www.cnblogs.com/ncore/p/3225590.html
Copyright © 2011-2022 走看看