C# 用zip,批量压缩文件,并打包下载

C# 用zip,批量压缩文件,并打包下载,无需产生临时文件,二种都可以

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;

 private void DownLoadZipFile()
        {

            DataTable dt = this.sqlca.GetDataTable("SELECT * FROM dbo.BPM_Attach WHERE lngRequestID = 1");

            using (MemoryStream stream = new MemoryStream())
            {
                using (ZipOutputStream zipStream = new ZipOutputStream(stream))//ZipOutputStream zipStream = new ZipOutputStream(File.Create("F:\\111\\111.zip"));
                {
                    zipStream.SetLevel(6);

                    var ent = new ZipEntry("/");
                    zipStream.PutNextEntry(ent);
                    zipStream.Flush();
                    Crc32 crc = new Crc32();

                    foreach (DataRow dr in dt.Rows)
                    {
                        var fileName = dr["strTitle"].ToString();
                        var fileContent = (byte[])dr["ImgContent"];

                        crc.Reset();
                        crc.Update(fileContent);

                        ent = new ZipEntry(fileName);
                        ent.DateTime = DateTime.Now;
                        ent.Size = fileContent.Length;
                        ent.Crc = crc.Value;

                        zipStream.PutNextEntry(ent);
                        zipStream.Write(fileContent, 0, fileContent.Length);
                    }

                    zipStream.Finish();
                    stream.Position = 0;

                    Response.Buffer = true;
                    Response.AddHeader("Content-Disposition", $"attachment; filename=附件_{DateTime.Now.ToString("yyMMddHHmmssfff")}.zip");
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(stream.ToArray());

                    zipStream.Close();
                    stream.Close();
                    GC.Collect();
                    GC.Collect(1);
                }
            }
        }


private void DownLoadZipFile2()
        {

            DataTable dt = this.sqlca.GetDataTable("SELECT * FROM dbo.BPM_Attach WHERE lngRequestID = 1");
            byte[] buffer = new byte[2048];

            using (MemoryStream stream = new MemoryStream())
            {
                using (ZipOutputStream zipStream = new ZipOutputStream(stream))//ZipOutputStream zipStream = new ZipOutputStream(File.Create("F:\\111\\111.zip"));
                {
                    zipStream.SetLevel(6);

                    var ent = new ZipEntry("/");
                    zipStream.PutNextEntry(ent);
                    zipStream.Flush();
                    Crc32 crc = new Crc32();

                    foreach (DataRow dr in dt.Rows)
                    {
                        var fileName = dr["strTitle"].ToString();
                        var fileContent = (byte[])dr["ImgContent"];
                        var streamInput = new MemoryStream(fileContent);

                        zipStream.PutNextEntry(new ZipEntry(fileName));
                        while (true)
                        {
                            var readCount = streamInput.Read(buffer, 0, buffer.Length);
                            if (readCount > 0)
                            {
                                zipStream.Write(buffer, 0, readCount);
                            }
                            else
                            {
                                break;
                            }
                        }
                        zipStream.Flush();
                    }

                    zipStream.Finish();
                    stream.Position = 0;

                    Response.Buffer = true;
                    Response.AddHeader("Content-Disposition", $"attachment; filename=附件_{DateTime.Now.ToString("yyMMddHHmmssfff")}.zip");
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(stream.ToArray());

                    zipStream.Close();
                    stream.Close();
                    GC.Collect();
                    GC.Collect(1);
                }
            }
        }

你可能感兴趣的:(c#)