asp.netsystem.io.packaging

ASP.Net : system.io.packaging package "File contains corrupted data"


I am using system.io.packaging.package to create a package containing some files. Purpose is to create an import/export functionnality.

I create the package like that :

        // Create the compressed file.
        using (FileStream outFile = File.Create(this.packageName))
        {
            using (Package Compress = Package.Open(outFile, FileMode.Create))
            {
                foreach (string file in Directory.GetFiles(Path.GetDirectoryName(this.packageName)))
                {
                    FileInfo fi = new FileInfo(file);

                    // Prevent compressing hidden and already compressed files.
                    if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fi.Extension != ".gz")
                    {
                        PackagePart packagePartDocument;

                        if (fi.Extension == ".xml")
                        {
                            packagePartDocument = Compress.CreatePart(PackUriHelper.CreatePartUri(new Uri(@"/" + Path.GetFileName(file), UriKind.Relative)), System.Net.Mime.MediaTypeNames.Text.Xml);
                        }
                        else
                        {
                            packagePartDocument = Compress.CreatePart(PackUriHelper.CreatePartUri(new Uri(@"/" + Path.GetFileName(file), UriKind.Relative)), System.Net.Mime.MediaTypeNames.Text.Plain);
                        }

                        using (FileStream inFile = fi.OpenRead())
                        {
                            inFile.CopyTo(packagePartDocument.GetStream());
                        }

                        Compress.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, fi.FullName);
                    }
                }
            }
        }

and I read it like that :

using (Package zip = Package.Open(PackageStream, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            Directory.CreateDirectory(Path.Combine(App.Configuration.TempDirectory, this.packageName.Split('.')[0]));
            foreach (PackagePart file in zip.GetParts())
            {
                string fileName = Path.Combine(App.Configuration.TempDirectory, this.packageName.Split('.')[0], file.Uri.ToString().TrimStart('/'));
                using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
                {
                    file.GetStream().CopyTo(fileStream);
                }
            }
        }

My issue occurs when Package try to open the file

Package zip = Package.Open(PackageStream, FileMode.Open, FileAccess.Read, FileShare.None)

I have an error "File contains corrupted data"

I have done many tests over and over and I don't understand...

EDIT : The same code is working with an console applciation but not in a web application.


Solution

  • Finally I found my mistake :

    When I return this generated package I return the package but the entire web page too.

        Response.AddHeader("content-disposition", "attachment; filename=" + export.PackageName);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(export.PackageFullName);
    

    So I corrected it by

        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=" + export.PackageName);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(export.PackageFullName);
        Response.Flush();
        Response.End();
    

    It works :)