Code-Helper:ZipHelper.cs

情到浓时终转凉″ 提交于 2020-08-13 10:32:59
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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!