夜的第七章

WEB开发个人博客。

« Intel cpu产品发布图Js对输入数字的疏忽导致错误 »

Asp.Net在线压缩解压

第一种方法调用winrar软件来实现.优点:目录层次清晰,与原来结构一样.缺点:如压缩深目录内的文件夹,目录路径也会压进去.没找到解决办法.
1.引用


using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
/******************************************************    
程序用途:实现文件[文件夹]压缩解压功能函数
程序备注:
 * 服务器端WinRAR支持
 * 路径简述必须绝对路径
******************************************************/



2.主要代码之压缩


   /// <summary>
   /// 压缩文件
   /// </summary>
   /// <param name="DFilePath">需要压缩的文件夹或者单个文件</param>
   /// <param name="DRARName">生成压缩文件的文件名</param>
   /// <param name="DRARPath">生成压缩文件保存路径</param>
   /// <returns></returns>
    protected bool RAR(string DFilePath, string DRARName,string DRARPath)
    {
        String the_rar;
        RegistryKey the_Reg;
        Object the_Obj;
        String the_Info;
        ProcessStartInfo the_StartInfo;
        Process the_Process;
        try
        {
            the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
            the_Obj = the_Reg.GetValue("");
            the_rar = the_Obj.ToString();
            the_Reg.Close();
            the_rar = the_rar.Substring(1, the_rar.Length - 7);
            the_Info = " a    " + " " + DRARName + "  " + DFilePath; //命令 + 压缩后文件名 + 被压缩的文件或者路径
            the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = the_rar;
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_StartInfo.WorkingDirectory = DRARPath ; //RaR文件的存放目录。
            the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }



3.主要代码之解压


    /// <summary>
    /// 解压缩到指定文件夹 
    /// </summary>
    /// <param name="RARFilePath">压缩文件存在的目录 </param>
    /// <param name="RARFileName">压缩文件名称 </param>
    /// <param name="UnRARFilePath">解压到文件夹</param>
    /// <returns></returns>
    protected bool UnRAR(string RARFilePath,string RARFileName,string UnRARFilePath)
    {
        //解压缩
        String the_rar;
        RegistryKey the_Reg;
        Object the_Obj;
        String the_Info;
        ProcessStartInfo the_StartInfo;
        Process the_Process;
        try
         {
            the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
            the_Obj = the_Reg.GetValue("");
            the_rar = the_Obj.ToString();
            the_Reg.Close();
            the_rar = the_rar.Substring(1, the_rar.Length - 7);
            the_Info = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
            the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = the_rar;
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            return true;
        }
        catch (Exception ex)
         {
             return false;
         }

    }




3.示例
        string oFilePath = @"C:\1\";
        string oRARFileName = "1.rar";
        string oToFilePath = @"C:\2\";

        //if (RAR(oFilePath, oRARFileName, oToFilePath))

        if (UnRAR(oFilePath, oRARFileName, oToFilePath))
            Response.Write("OK");
        else
            Response.Write("No"); 


第二种是用.net自己的类.缺点:目录不清晰,一个目录一压就成一个文件了.优点:压缩无目录痕迹(压缩后就成一个文件了,哪还有痕迹?555).
gzipstream的包装类(转载)
namespace GreatCHN.GZipCompression
{
    public class GZipCompress
    {
        /// <summary>
        /// 对目标文件夹进行压缩,将压缩结果保存为指定文件
        /// </summary>
        /// <param name="dirPath">目标文件夹</param>
        /// <param name="fileName">压缩文件</param>
        public static void Compress(string dirPath, string fileName)
        {
            ArrayList list = new ArrayList();
            foreach (string f in Directory.GetFiles(dirPath))
            {
                byte[] destBuffer = File.ReadAllBytes(f);
                SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer);
                list.Add(sfi);
            }
            IFormatter formatter = new BinaryFormatter();
            using (Stream s = new MemoryStream())
            {
                formatter.Serialize(s, list);
                s.Position = 0;
                CreateCompressFile(s, fileName);
            }
        }

        /**/
        /// <summary>
        /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
        /// </summary>
        /// <param name="fileName">压缩文件</param>
        /// <param name="dirPath">解压缩目录</param>
        public static void DeCompress(string fileName, string dirPath)
        {
            using (Stream source = File.OpenRead(fileName))
            {
                using (Stream destination = new MemoryStream())
                {
                    using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
                    {
                        byte[] bytes = new byte[4096];
                        int n;
                        while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            destination.Write(bytes, 0, n);
                        }
                    }
                    destination.Flush();
                    destination.Position = 0;
                    DeSerializeFiles(destination, dirPath);
                }
            }
        }

        private static void DeSerializeFiles(Stream s, string dirPath)
        {
            BinaryFormatter b = new BinaryFormatter();
            ArrayList list = (ArrayList)b.Deserialize(s);

            foreach (SerializeFileInfo f in list)
            {
                string newName = dirPath + Path.GetFileName(f.FileName);
                using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(f.FileBuffer, 0, f.FileBuffer.Length);
                    fs.Close();
                }
            }
        }

        private static void CreateCompressFile(Stream source, string destinationName)
        {
            using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
            {
                using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
                {
                    byte[] bytes = new byte[4096];
                    int n;
                    while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        output.Write(bytes, 0, n);
                    }
                }
            }
        }

        [Serializable]
        class SerializeFileInfo
        {
            public SerializeFileInfo(string name, byte[] buffer)
            {
                fileName = name;
                fileBuffer = buffer;
            }

            string fileName;
            public string FileName
            {
                get
                {
                    return fileName;
                }
            }

            byte[] fileBuffer;
            public byte[] FileBuffer
            {
                get
                {
                    return fileBuffer;
                }
            }
        }
    }
} 



两种方法各有优略吧.

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。



[2007 - 2011] © Leadnt.com