如何实现多文件压缩 

解决方案 »

  1.   

    用icsharpcodehttp://www.cnblogs.com/papapa/articles/751927.html
      

  2.   

    下面是我实现的。protected void OnZip(object sender, EventArgs e)
    {
        string path = base.Server.MapPath(this.tbZipFolder.Text);
        if (Directory.Exists(path))
        {
            if (!path.EndsWith(@"\"))
            {
                path = path + @"\";
            }
            try
            {
                string str2 = base.Server.MapPath(this.tbZipFolderTo.Text);
                if (!Directory.Exists(Path.GetDirectoryName(str2)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(str2));
                }
                string[] strArray = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
                string[] strArray2 = Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
                using (ZipOutputStream stream = new ZipOutputStream(File.Create(str2)))
                {
                    ZipEntry entry;
                    stream.SetLevel(9);
                    byte[] buffer = new byte[0x1000];
                    foreach (string str3 in strArray2)
                    {
                        entry = new ZipEntry(str3.Remove(0, path.Length) + @"\");
                        entry.set_DateTime(DateTime.Now);
                        stream.PutNextEntry(entry);
                    }
                    foreach (string str4 in strArray)
                    {
                        entry = new ZipEntry(str4.Remove(0, path.Length));
                        entry.set_DateTime(DateTime.Now);
                        stream.PutNextEntry(entry);
                        using (FileStream stream2 = File.OpenRead(str4))
                        {
                            int num;
                            do
                            {
                                num = stream2.Read(buffer, 0, buffer.Length);
                                stream.Write(buffer, 0, num);
                            }
                            while (num > 0);
                        }
                    }
                    stream.Finish();
                    stream.Close();
                }
                this.labelMsg.Text = string.Format("压缩目录[{0}]到文件[{1}]成功", this.tbZipFolder.Text, this.tbZipFolderTo.Text);
            }
            catch (Exception exception)
            {
                this.labelMsg.Text = exception.Message;
            }
        }
        else
        {
            this.labelMsg.Text = string.Format("目录[{0}]不存在", this.tbZipFolder.Text);
        }
    }