如题,要求:
和Winrar压缩工具中的“标准”方式压缩出来的一样的效果。
下面是我写的压缩代码,但是我上传到某个服务器上,后台解压的时候报错,说解压格式不匹配,我不知道后台如何处理,但是我用Winrar手动压缩后的格式后台可以解压,是不是我的压缩代码的问题?using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
public void ZipFilenew(string strFileFolder, string strZip)
        {
            if (strFileFolder[strFileFolder.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                strFileFolder += System.IO.Path.DirectorySeparatorChar;
            ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
            s.SetLevel(8); // 0 - store only to 9 - means best compression,10种压缩方式我都试过,都不行
            zip(strFileFolder, strZip, s);
            s.Finish();
            s.Close();
        }
        private void zip(string strFile, string StoreFile, ZipOutputStream s)
        {
            if (strFile[strFile.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                strFile += System.IO.Path.DirectorySeparatorChar;
            Crc32 crc = new Crc32();            string[] filenames = Directory.GetFileSystemEntries(strFile);
            foreach (string file in filenames)
            {
                if (Directory.Exists(file))
                {
                    zip(file, StoreFile, s);
                }
                else
                {
                    //打开压缩文件,开始压缩
                    FileStream fs = File.OpenRead(file);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string tempfile = file.Substring(file.LastIndexOf(aaa+"\\") + aaa.Length + 1);//实现子文件夹的压缩
                    ZipEntry entry = new ZipEntry(tempfile);
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
            }
            
        }

解决方案 »

  1.   

    给你做个参考,不久前我项目中用到的.#region 压缩与解压
            /// <summary>
            /// 压缩文件夹
            /// </summary>
            private void modZip()
            {
                //压缩/解压ZIP包时读取文件的缓冲区大小
                int int_zipBufferSize = int.Parse(ConfigurationManager.AppSettings["zipBufferSize"].ToString());
                //压缩品质级别(0~9)
                int CompressionLevel = int.Parse(ConfigurationManager.AppSettings["zipLevel"].ToString());            //XML文件临时保存路径
                string str_Path = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["fileDefaultPath"].ToString();
                DirectoryInfo di = new DirectoryInfo(str_Path);            //目标文件名
                string str_GzipFileName = str_zipPath;            ////另存为的时候需要临时创建文件
                //if (File.Exists(str_GzipFileName) == false)
                //{
                //    //创建文件
                //    using (FileStream fs = File.Create(str_GzipFileName))
                //    {
                //        fs.Close();
                //    }
                //}            //打开ZIP文件以进行写入
                Stream zipFilestream = File.OpenWrite(str_GzipFileName);
                ZipOutputStream stream = new ZipOutputStream(zipFilestream);            try
                {                if (str_Path[str_Path.Length - 1] != Path.DirectorySeparatorChar)
                    {
                        str_Path += Path.DirectorySeparatorChar;
                    }
                    stream.SetLevel(CompressionLevel);
                    zip(str_Path, stream, str_Path);
                    stream.Finish();
                    stream.Close();
                }
                catch (Exception ex)
                {
                    //记录错误日志   
                    log.Error("error", new Exception(ex.ToString()));
                }
                finally
                {
                    stream.Finish();
                    stream.Close();
                }
            }        private void zip(string strFile, ZipOutputStream s, string staticFile)
            {
                try
                {
                    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
                    string[] filenames = Directory.GetFileSystemEntries(strFile);
                    foreach (string file in filenames)
                    {                    if (Directory.Exists(file))
                        {
                            zip(file, s, staticFile);
                        }
                        else // 否则直接压缩文件
                        {
                            //打开压缩文件
                            FileStream fs = File.OpenRead(file);                        byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);
                            string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                            ZipEntry entry = new ZipEntry(tempfile);                        entry.DateTime = DateTime.Now;
                            entry.Size = fs.Length;
                            fs.Close();
                            s.PutNextEntry(entry);                        s.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    //记录错误日志   
                    log.Error("error", new Exception(ex.ToString()));
                }
            }        /// <summary>
            /// 解压缩文件
            /// </summary>
            /// <param name="str_zipPath">压缩包路径</param>     
            private void modExtract(string str_zipPath)
            {
                try
                {
                    //压缩/解压ZIP包时读取文件的缓冲区大小
                    int int_zipBufferSize = int.Parse(ConfigurationManager.AppSettings["zipBufferSize"].ToString());                //解压缩目标路径,解压至临时文件夹
                    string CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["fileDefaultPath"].ToString();                if (File.Exists(str_zipPath) == false)
                    {
                        return;
                    }                //创建解压目录
                    if (Directory.Exists(CurrentDirectory) == true)
                    {
                        Directory.Delete(CurrentDirectory, true);
                    }
                    Directory.CreateDirectory(CurrentDirectory);                string rootFile = " ";
                    //读取压缩文件(zip文件),准备解压缩
                    string path = CurrentDirectory;
                    //解压出来的文件保存的路径
                    FileStream fs = File.OpenRead(str_zipPath);
                    using (ZipInputStream s = new ZipInputStream(fs))
                    {
                        if (s.Length == 0)
                        {
                            return;
                        }
                        string rootDir = " ";
                        ZipEntry theEntry = s.GetNextEntry();
                        //根目录下的第一个子文件夹的名称
                        while (theEntry != null)
                        {
                            rootDir = Path.GetDirectoryName(theEntry.Name);
                            //得到根目录下的第一级子文件夹的名称
                            if (rootDir.IndexOf("\\") >= 0)
                            {
                                rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
                            }
                            string dir = Path.GetDirectoryName(theEntry.Name);
                            //根目录下的第一级子文件夹的下的文件夹的名称
                            string fileName = Path.GetFileName(theEntry.Name);
                            //根目录下的文件名称
                            if (dir != " ")
                            //创建根目录下的子文件夹,不限制级别
                            {
                                if (!Directory.Exists(CurrentDirectory + "\\" + dir))
                                {
                                    path = CurrentDirectory + "\\" + dir;
                                    //在指定的路径创建文件夹
                                    Directory.CreateDirectory(path);
                                }
                            }
                            else if (dir == " " && fileName != "")
                            //根目录下的文件
                            {
                                path = CurrentDirectory;
                                rootFile = fileName;
                            }
                            else if (dir != " " && fileName != "")
                            //根目录下的第一级子文件夹下的文件
                            {
                                if (dir.IndexOf("\\") > 0)
                                //指定文件保存的路径
                                {
                                    path = CurrentDirectory + "\\" + dir;
                                }
                            }
                            if (dir == rootDir)
                            //判断是不是需要保存在根目录下的文件
                            {
                                path = CurrentDirectory + "\\" + rootDir;
                            }
                            //以下为解压缩zip文件的基本步骤
                            //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
                            if (fileName != String.Empty)
                            {
                                FileStream streamWriter = File.Create(path + "\\" + fileName);
                                int size = int_zipBufferSize;
                                byte[] data = new byte[int_zipBufferSize];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                streamWriter.Close();
                            }
                            theEntry = s.GetNextEntry();
                        }
                        s.Close();
                    }            }
                catch (Exception ex)
                {
                    //记录错误日志   
                    log.Error("error", new Exception(ex.ToString()));
                }
            }
            #endregion