• Code-Helper:ZipHelper.cs


    ylbtech-Code-Helper:ZipHelper.cs
    1.返回顶部
    1、
    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.IO.Compression;
    using System.IO;
    using System.Diagnostics;
    using System.Xml;
    
    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 Sp.DBUtility
    {
        public class ZipHelper
        {
            public void ZipFile(string fileToZip, string zippedFile)
            {
                if (string.IsNullOrEmpty(fileToZip))
                    throw new ArgumentNullException("fileToZip");
    
                using (ZipOutputStream stream = new ZipOutputStream(File.Create(zippedFile)))
                {
                    using (FileStream fs = File.OpenRead(fileToZip))
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(fileToZip));
                        entry.DateTime = DateTime.UtcNow;
                        entry.Size = fs.Length;
                        stream.PutNextEntry(entry);
                        int readLen = 0;
                        int maxlength = 10000;
                        byte[] buffer = new byte[maxlength];
                        readLen = fs.Read(buffer, 0, buffer.Length);
                        for (int i = 0; ; i++)
                        {
                            if (readLen > 0)
                            {
                                stream.Write(buffer, 0, readLen);
                                readLen = fs.Read(buffer, 0, buffer.Length);
                            }
                            else
                            {
                                break;
                            }
                        }
                        fs.Close();
                        stream.Close();
                    }
                }
            }
    
            public void ZipDir(string zippedDir, string zippedFile, bool recurse, string fileFilter)
            {
                ICSharpCode.SharpZipLib.Zip.FastZip fastZip = new ICSharpCode.SharpZipLib.Zip.FastZip();
                fastZip.CreateZip(zippedFile, zippedDir, recurse, fileFilter);
            }
    
            public byte[] Zip(string text, string zipEntryName)
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(text);
                return this.ZipBytes(inputByteArray, zipEntryName);
            }
    
            public byte[] ZipBytes(byte[] bytes, string zipEntryName)
            {
                byte[] inputByteArray = bytes;
                using (MemoryStream ms = new MemoryStream())
                {
                    // Check the #ziplib docs for more information
                    using (ZipOutputStream zipOut = new ZipOutputStream(ms))
                    {
                        ZipEntry ZipEntry = new ZipEntry(zipEntryName);
                        zipOut.PutNextEntry(ZipEntry);
                        zipOut.SetLevel(9);
                        zipOut.Write(inputByteArray, 0, inputByteArray.Length);
                        zipOut.Finish();
                        zipOut.Close();
    
                        // Return the zipped contents
                        return ms.ToArray();
                    }
                }
            }
        }
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
    1、ICSharpCode.SharpZipLib.dll
    2、
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    JS来推断文本框内容改变事件
    LINQ To SQL 语法及实例大全
    linux-多线程
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
    用Jfree实现条形柱状图表,java代码实现
    OpenGL中glPushMatrix和glPopMatrix的原理
    C# 之 抽象类与接口
    Android漫游记(1)---内存映射镜像(memory maps)
    Web页面布局方式小结
    STL中主要的算法(一)
  • 原文地址:https://www.cnblogs.com/storebook/p/13087271.html
Copyright © 2020-2023  润新知