我用GZipStream压缩文件夹后,发现原来文件夹中的文件扩展名没了,怎么办啊????这是代码: 这是文件夹路径  string path = Server.MapPath("../../../UpLoads/Attachment/" + attachment);
 这是压缩后文件路径  string fileName =path + ".rar";
  
 Compress(path, fileName);
  public  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);
        }
    } private  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]
   public 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;
            }
        }    }

解决方案 »

  1.   


    一股脑地把 byte[] 堆成一堆去了?!改变你的 list 类型设计,让它保存文件名。
      

  2.   

     using (FileStream fs = new FileStream(filepath,FileMode.Open))
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                
                using (FileStream outfile = File.Create(Path.ChangeExtension(filepath, "zip")))
                {
                    using (GZipStream zipStream = new GZipStream(outfile, CompressionMode.Compress))
                    {
                        zipStream.Write(bytes, 0, bytes.Length);
                    }
                
                }
            }
    还可使用SharpZipLib解压缩
    private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
            {
                bool res = true;
                string[] folders, filenames;
                ZipEntry entry = null;
                FileStream fs = null;
                Crc32 crc = new Crc32();
                try
                {
                    entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));
                    s.PutNextEntry(entry);
                    s.Flush();
                    filenames = Directory.GetFiles(FolderToZip);
                    foreach (string file in filenames)
                    {
                        fs = File.OpenRead(file);                    byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));                    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);
                    }
                }
                catch
                {
                    res = false;
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                        fs = null;
                    }
                    if (entry != null)
                    {
                        entry = null;
                    }
                    GC.Collect();
                    GC.Collect(1);
                }
                folders = Directory.GetDirectories(FolderToZip);
                foreach (string folder in folders)
                {
                    if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
                    {
                        return false;
                    }
                }            return res;
            }
            private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
            {
                bool res;
                if (!Directory.Exists(FolderToZip))
                {
                    return false;
                }            ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
                s.SetLevel(6);
                s.Password = Password;            res = ZipFileDictory(FolderToZip, s, "");            s.Finish();
                s.Close();            return res;
            }
            private static bool ZipFile(string FileToZip, string ZipedFile, String Password)
            {
                if (!File.Exists(FileToZip))
                {
                    throw new System.IO.FileNotFoundException("压缩的文件: " + FileToZip + " 不存在!");
                }
                FileStream ZipFile = null;
                ZipOutputStream ZipStream = null;
                ZipEntry ZipEntry = null;            bool res = true;
                try
                {
                    ZipFile = File.OpenRead(FileToZip);
                    byte[] buffer = new byte[ZipFile.Length];
                    ZipFile.Read(buffer, 0, buffer.Length);
                    ZipFile.Close();                ZipFile = File.Create(ZipedFile);
                    ZipStream = new ZipOutputStream(ZipFile);
                    ZipStream.Password = Password;
                    ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
                    ZipStream.PutNextEntry(ZipEntry);
                    ZipStream.SetLevel(6);                ZipStream.Write(buffer, 0, buffer.Length);
                }
                catch
                {
                    res = false;
                }
                finally
                {
                    if (ZipEntry != null)
                    {
                        ZipEntry = null;
                    }
                    if (ZipStream != null)
                    {
                        ZipStream.Finish();
                        ZipStream.Close();
                    }
                    if (ZipFile != null)
                    {
                        ZipFile.Close();
                        ZipFile = null;
                    }
                    GC.Collect();
                    GC.Collect(1);
                }
                return res;
            }
    参考