6

I created a BizTalk Custom Pipeline Component, which zips all message parts into a zip stream. After some fails I created the following test method in a separate test project.

Basically I get a XML file which contains a filename and an UUID which I use to call a stored procedure and get the Base64 encoded content of the database entry. The base64 content seems valid, because after decoding it and saving it to the file system the files can be read by the windows explorer without problems.

After saving the archiveStream to the file system I get the following error message from 7Zip when I try to extract the file: "Unexpected end of data". if I try to just open the file with 7Zip there is no problem. I even can open the files from inside the 7Zip explorer.

If I try to read the file from C# with the following code I get the error message: "End of Central Directory record could not be found."

Unzip code:

    private static void ReadDat() {

        var path = @"...\zip\0e00128b-0a6e-4b99-944d-68e9c20a51c2.zip";

        var stream = System.IO.File.OpenRead(path);
        // End of Central Directory record could not be found:
        var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read, false);
        foreach(var zipEntry in zipArchive.Entries) {

            var stream = zipEntry.Open();
            Console.WriteLine(stream.Length);

        }

    }

Zip Code:

    private static void StreamUuidList() {

        var path = @"...\2017-08-05T132705.xml";
        var xdoc = XDocument.Load(System.IO.File.OpenRead(path));
        var files = xdoc.Root.Descendants().Where(d => d.Name.LocalName.Equals("NodeName"));

        using (var archiveStream = new MemoryStream())
        using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true)) {

            foreach (var file in files) {

                var fileName = file.Elements().Where(e => e.Name.LocalName.Equals("FileName")).FirstOrDefault()?.Value ?? "";
                var streamUuid = file.Elements().Where(e => e.Name.LocalName.Equals("StreamUUID")).FirstOrDefault()?.Value ?? "";                    

                // validation here...

                // get base64 content and convert content
                var base64Content = GetStreamContent(streamUuid);
                var data = Convert.FromBase64String(base64Content);
                var dataStream = new MemoryStream(data);
                dataStream.Seek(0, SeekOrigin.Begin);

                // debug - save to file location
                using (var fileStream = new FileStream($@"...\files\{fileName}", FileMode.Create)) {

                    dataStream.CopyTo(fileStream);

                }
                dataStream.Seek(0, SeekOrigin.Begin);

                // create zip entry
                var zipFile = archive.CreateEntry(fileName, GetCompressionLevelFromString("Optimal"));
                using (var zipFileStream = zipFile.Open()) {

                    // copy data from mesage part stream into zip entry stream
                    dataStream.Seek(0, SeekOrigin.Begin);
                    dataStream.CopyTo(zipFileStream);

                }

                Console.WriteLine(fileName + ": " + streamUuid);

            }

            // debug - save to file location
            archiveStream.Seek(0, SeekOrigin.Begin);
            using (var fileStream = new FileStream($@"...\zip\{Guid.NewGuid()}.dat", FileMode.Create)) {

                archiveStream.CopyTo(fileStream);

            }
            // debug end

        }

    }
Mario
  • 978
  • 2
  • 11
  • 31

0 Answers0