Code-Helper:ZipHelper.cs

2020/06/11 13:06
阅读数 35
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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部