原创  IsharpCode 创建压缩文件,压入 字符串文本,压入 dataset 收藏

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Xml;
using System.Text;
/// <summary>
/// ZipHelper 的摘要说明,yeness
/// </summary>
public class ZipHelper
{
    public ZipHelper()
    {

    }
    /// <summary>
    /// 增加一个空rar文件,J#方法
    /// </summary>
    /// <param name="zipFileName">新建rar名称</param>
    /// <param name="zipFilePath">新建rar路径</param>
    public static void CreateRar(string zipFileName, string zipFilePath)
    {
        try
        {
            new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(zipFilePath + "\\" + zipFileName + ".rar")).close();
            new java.util.zip.ZipFile(zipFilePath + "\\" + zipFileName+".rar");
        }
        catch (Exception ex)
        {
            throw new System.NotImplementedException(ex.Message);
        }
    }
    /// <summary>
    /// 增加一个空rar文件,ICSharp方法
    /// </summary>
    /// <param name="zipFileName"></param>
    /// <param name="zipFilePath"></param>
    public static void CreateRarICSharp(string zipFileName, string zipFilePath)
    {
        try
        {
            string zipFile = zipFilePath + "\\" + zipFileName + ".rar";
            MemoryStream ms = new MemoryStream();
            //byte[] data = new byte[2048];
            ZipOutputStream zipOutputStream = new ZipOutputStream(ms);

            ZipEntry zipEntry = new ZipEntry("temp");
            zipEntry.DateTime = DateTime.Now;
            zipOutputStream.PutNextEntry(zipEntry);

            string str = " ";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);

            int sourceBytes;
            do
            {
                sourceBytes = ms.Read(buffer, 0, buffer.Length);
                zipOutputStream.Write(buffer, 0, buffer.Length);
            }
            while (sourceBytes > 0);
            zipOutputStream.Close();
            zipOutputStream.Finish();
            using (FileStream fileStream = new FileStream(zipFile, FileMode.Create, FileAccess.Write))
            {
                byte[] b = ms.ToArray();
                fileStream.Write(b, 0, b.Length);
            }
        }
        catch (Exception ex)
        {
            throw new System.NotImplementedException(ex.Message);
        }
    }


    /// <summary>
    /// 向rar文件添加一个文件
    /// </summary>
    /// <param name="zipFilePath">rar路径</param>
    /// <param name="newZipTxtName">新建压缩文本名称</param>
    /// <param name="newZipTxtContent">新建压缩文本内容</param>
    public static void AddTxt(string zipFilePath, string newZipTxtName, string newZipTxtContent)
    {
        try
        {
            byte[] buffer;//rar文件

            using (FileStream fStream = new FileStream(zipFilePath, FileMode.Open))//打开压缩流
            {
                buffer = new byte[fStream.Length];
                fStream.Read(buffer, 0, buffer.Length);//读取到缓冲区
            }

            MemoryStream msOutputStream = new MemoryStream();//压缩内存流
            ZipOutputStream zipOutputStream = new ZipOutputStream(msOutputStream);//定义一个压缩流对象

            MemoryStream msInputStream = new MemoryStream(buffer);//解压缩内存流,缓冲区转换成内存流
            ZipInputStream zipInputStream = new ZipInputStream(msInputStream);//内存流转换成解压缩流
          
            ZipEntry entry;
            byte[] data = new byte[2048];
            while ((entry = zipInputStream.GetNextEntry()) != null)//如果压缩文件内存在文件。
            {
                ZipEntry zipEntry = new ZipEntry(entry.Name);//压缩文件的配置信息
                zipEntry.DateTime = DateTime.Now;//压缩时间
                zipOutputStream.PutNextEntry(zipEntry);//压缩内存流读入文件信息
               
                int sourceBytes;
                do
                {
                    sourceBytes = zipInputStream.Read(data, 0, data.Length);//解压原rar文件
                    zipOutputStream.Write(data, 0, sourceBytes);//读入到压缩流
                }
                while (sourceBytes > 0);
            }

            //读入文本
            ZipEntry zipentry = new ZipEntry(newZipTxtName);
            zipentry.DateTime = DateTime.Now;//文件时间
            zipOutputStream.PutNextEntry(zipentry);//压缩流记入

            byte[] b = System.Text.Encoding.UTF8.GetBytes(newZipTxtContent);
            int sBytes;
            do
            {
                sBytes = msOutputStream.Read(b, 0, b.Length);
                zipOutputStream.Write(b, 0, b.Length);
            }
            while (sBytes > 0);
            ////读入文本结束
            zipOutputStream.Close();
            zipOutputStream.Finish();
            using (FileStream fStream = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
            {//重新建立压缩文件
                byte[] e = msOutputStream.ToArray();
                fStream.Write(e, 0, e.Length);
                //fStream.Seek(0, SeekOrigin.Begin);
            }
        }
        catch (Exception ex)
        {
            throw new System.NotImplementedException(ex.Message);
        }
    }

    /// <summary>
    /// 向rar文件添加一个文件
    /// </summary>
    /// <param name="zipFilePath">rar路径</param>
    /// <param name="newZipTxtName">新建压缩文本名称</param>
    /// <param name="newZipTxtContent">新建压缩文本内容</param>
    public static void AddDataset(string zipFilePath,DataSet ds)
    {
        try
        {
            byte[] buffer;//rar文件

            using (FileStream fStream = new FileStream(zipFilePath, FileMode.Open))//打开压缩流
            {
                buffer = new byte[fStream.Length];
                fStream.Read(buffer, 0, buffer.Length);//读取到缓冲区
            }

            MemoryStream msOutputStream = new MemoryStream();//压缩内存流
            ZipOutputStream zipOutputStream = new ZipOutputStream(msOutputStream);//定义一个压缩流对象

            MemoryStream msInputStream = new MemoryStream(buffer);//解压缩内存流,缓冲区转换成内存流
            ZipInputStream zipInputStream = new ZipInputStream(msInputStream);//内存流转换成解压缩流

            ZipEntry entry;
            byte[] data = new byte[2048];
            while ((entry = zipInputStream.GetNextEntry()) != null)//如果压缩文件内存在文件。
            {
                ZipEntry zipEntry = new ZipEntry(entry.Name);//压缩文件的配置信息
                zipEntry.DateTime = DateTime.Now;//压缩时间
                zipOutputStream.PutNextEntry(zipEntry);//压缩内存流读入文件信息

                int sourceBytes;
                do
                {
                    sourceBytes = zipInputStream.Read(data, 0, data.Length);//解压原rar文件
                    zipOutputStream.Write(data, 0, sourceBytes);//读入到压缩流
                }
                while (sourceBytes > 0);
            }

            //读入文本
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                ZipEntry zipentry = new ZipEntry(ds.Tables[i].TableName);
                zipentry.DateTime = DateTime.Now;//文件时间
                zipOutputStream.PutNextEntry(zipentry);//压缩流记入

                byte[] b =System.Text.Encoding.UTF8.GetBytes(ConvertDataTableToXML(ds.Tables[i]));
                int sBytes;
                do
                {
                    sBytes = msOutputStream.Read(b, 0, b.Length);
                    zipOutputStream.Write(b, 0, b.Length);
                }
                while (sBytes > 0);
            }
            ////读入文本结束
            zipOutputStream.Close();
            zipOutputStream.Finish();
            using (FileStream fStream = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
            {//重新建立压缩文件
                byte[] e = msOutputStream.ToArray();
                fStream.Write(e, 0, e.Length);
                //fStream.Seek(0, SeekOrigin.Begin);
            }
        }
        catch (Exception ex)
        {
            throw new System.NotImplementedException(ex.Message);
        }
    }

    private static string ConvertDataTableToXML(DataTable xmlDS)
    {
        MemoryStream stream = null;
        XmlTextWriter writer = null;
        try
        {
            stream = new MemoryStream();
            writer = new XmlTextWriter(stream, Encoding.UTF8);
            xmlDS.WriteXml(writer);
            int count = (int)stream.Length;
            byte[] arr = new byte[count];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(arr, 0, count);
            string sssss = Encoding.UTF8.GetString(arr).Trim();
            return sssss;
        }
        catch
        {
            return String.Empty;
        }
        finally
        {
            if (writer != null) writer.Close();
        }
    }

}

发表于 @ 2009年03月09日 14:53:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:div panel frame 高度 宽度 | 新一篇:list泛型排序

  • 发表评论
  • 评论内容:
  •  
Copyright © yeness
Powered by CSDN Blog