Beispiel #1
0
            public static void WriteZip(string packagePath, string[] filePathes, string[] contentTypes, HttpResponse response, string fileName)
            {
                if (filePathes.Length != contentTypes.Length)
                {
                    throw new ArgumentException("The lengths of the number of files and the content types should be equal");
                }

                response.Clear();
                response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".zip");
                response.ContentType = "application/zip";

                using (ZipPackage package = (ZipPackage)Package.Open(packagePath, FileMode.Create))
                {
                    for (int i = 0; i < filePathes.Length; i++)
                    {
                        string destFilename = ".\\" + Path.GetFileName(filePathes[i]);
                        Uri    uri          = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.RelativeOrAbsolute));
                        if (package.PartExists(uri))
                        {
                            package.DeletePart(uri);
                        }
                        PackagePart part = package.CreatePart(uri, "", CompressionOption.Normal);
                        using (FileStream fileStream = new FileStream(filePathes[i], FileMode.Open, FileAccess.Read))
                        {
                            using (Stream dest = part.GetStream())
                            {
                                CopyStream(fileStream, dest);
                            }
                        }
                    }

                    package.Flush();
                }

                response.Write(packagePath);

                response.Flush();
                response.Close();
            }