用下面的代码压缩,可以压缩但里面同时保留目录结构,一打开压缩包就看到h:点进去看到imagesys是文件名再点进去download再点进去看到GUID文件名再点进去真正看到图片了,够晕了要点这么多次再能看到想要的图片,原来图片的路径随机生成在h:\imagesys\download\guid\2008.jpeg
现在的问题是有没有办法直接把2008.jpeg压缩进去呢,路径就不要压了/// <summary>
    /// 压缩目录
    /// </summary>
    /// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>
    public static void ZipFileDictory(string[] args)
    {
        string[] filenames = Directory.GetFiles(args[0]);        Crc32 crc = new Crc32();
        ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
        s.SetLevel(6);        foreach (string file in filenames)
        {
            //打开压缩文件
            FileStream fs = File.OpenRead(file);            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            ZipEntry entry = new ZipEntry(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);        }        s.Finish();
        s.Close();
    }

解决方案 »

  1.   

    new ZipEntry时候传入的file是不是带路径的?
      

  2.   

    /// <summary> 
        /// 压缩目录 
        /// </summary> 
        /// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名) </param> 
        public static void ZipFileDictory(string[] args) 
        { 
            string[] filenames = Directory.GetFiles(args[0]);         Crc32 crc = new Crc32(); 
            ZipOutputStream s = new ZipOutputStream(File.Create(args[1])); 
            s.SetLevel(6);         foreach (string file in filenames) 
            { 
                //打开压缩文件 
                FileStream fs = File.OpenRead(file);             byte[] buffer = new byte[fs.Length]; 
                fs.Read(buffer, 0, buffer.Length);             int i = file.LastIndexOf(@"\");
                    string f;
                    if (i >= 0)
                    {
                        f = file.Substring(i + 1);
                    }
                    else
                    {
                        f=file ;
                    }
                ZipEntry entry = new ZipEntry(f); 
                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);         }         s.Finish(); 
            s.Close(); 
        }
      

  3.   

    public static void ZipFileDictory(string[] args) 
        { 
            string[] filenames = Directory.GetFiles(args[0]);         Crc32 crc = new Crc32(); 
            ZipOutputStream s = new ZipOutputStream(File.Create(args[1])); 
            s.SetLevel(6);         foreach (string file in filenames) 
            { 
                //打开压缩文件 
                FileStream fs = File.OpenRead(file);             byte[] buffer = new byte[fs.Length]; 
                fs.Read(buffer, 0, buffer.Length); 
                ZipEntry entry = new ZipEntry(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);         }         s.Finish(); 
            s.Close(); 
        }改称这样就可以了
      

  4.   

    真幸福
    可以用第三方的控件我却要自己写.....SharpZipLib.dll
    不能用于商业用途...
      

  5.   

    ericzhangbo1982111 ,很同情你.的确ICSharpCode.SharpZipLib.dll不能的,我有时候改个名字偷偷调用一下,呵呵.因为我们不是作通用软件的,都是内部软件.Path.GetFileName()我以前用过,突然就想不出来了,手边没有环境,只好写个笨办法了.
      

  6.   

    非常感谢各位,问题已解决了
    public static void ZipFileDictory(string[] args) 
        { 
            string[] filenames = Directory.GetFiles(args[0]);         Crc32 crc = new Crc32(); 
            ZipOutputStream s = new ZipOutputStream(File.Create(args[1])); 
            s.SetLevel(6);         foreach (string file in filenames) 
            { 
                //打开压缩文件 
                FileStream fs = File.OpenRead(file);             byte[] buffer = new byte[fs.Length]; 
                fs.Read(buffer, 0, buffer.Length); 
                ZipEntry entry = new ZipEntry(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);         }         s.Finish(); 
            s.Close(); 
        } 改称这样就可以了