码迷,mamicode.com
首页 > 其他好文 > 详细

Code-Helper:ZipHelper.cs

时间:2020-06-10 19:15:59      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:utf8   contents   memory   pen   返回   white   date   util   lan   

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.返回顶部
 
技术图片 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Code-Helper:ZipHelper.cs

标签:utf8   contents   memory   pen   返回   white   date   util   lan   

原文地址:https://www.cnblogs.com/storebook/p/13087271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!