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
  • 相关阅读:
    《JavaWeb从入门到改行》JSP+EL+JSTL大杂烩汤
    Linux下进程线程,Nignx与php-fpm的进程线程方式
    solr全文检索实现原理
    LSM树以及在hbase中的应用
    MySQL的MyISAM与InnoDB的索引方式
    MySQL的innoDB存储引擎的运作方式,数据结构等
    Redis作缓存
    Redis的几点积累
    Redis数据库各种数据结构的内部实现。
    正则表达式!!!
  • 原文地址:https://www.cnblogs.com/ncore/p/3225590.html
Copyright © 2011-2022 走看看