Search code examples
javascriptajaxasp.net-coresystem.io.compression

Can't open zip file created from System.IO.Compression namespace


I'm trying to zip varying amounts of files so that one zip folder can be served to the user instead of them having to click multiple anchor tags. I am using the System.IO.Compression namespace in asp.net core 3.1 to create the zip folder.

Here is the code I'm using to create the Zip folder.

        public IActionResult DownloadPartFiles(string[] fileLocations, string[] fileNames)
        {
            List<InMemoryFile> files = new List<InMemoryFile>();
            for (int i = 0; i < fileNames.Length; i++)
            {
                InMemoryFile inMemoryFile = GetInMemoryFile(fileLocations[i], fileNames[i]).Result;
                files.Add(inMemoryFile);
            }
            byte[] archiveFile;
            using (MemoryStream archiveStream = new MemoryStream())
            {
                using (ZipArchive archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
                {
                    foreach (InMemoryFile file in files)
                    {
                        ZipArchiveEntry zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Fastest);
                        using (Stream zipStream = zipArchiveEntry.Open())
                        {
                            zipStream.Write(file.Content, 0, file.Content.Length);
                            zipStream.Close();
                        }
                    }
                    archiveStream.Position = 0;
                }
                archiveFile = archiveStream.ToArray();
            }

            return File(archiveFile, "application/octet-stream");
        }

The files I am trying to zip are stored remotely so I grab them with this block of code. The InMemoryFile is a class to group the file name and file bytes together.

        private async Task<InMemoryFile> GetInMemoryFile(string fileLocation, string fileName)
        {
            InMemoryFile file;
            using (HttpClient client = new HttpClient())
            using (HttpResponseMessage response = await client.GetAsync(fileLocation))
            {
                byte[] fileContent = await response.Content.ReadAsByteArrayAsync();
                file = new InMemoryFile(fileName, fileContent);
            }
            return file;
        }

The DownloadPartFiles method is called using Ajax. I grab the remote paths to the files and their respective names using javascript and pass them into the Ajax call.

function downloadAllFiles() {
    let partTable = document.getElementById("partTable");
    let linkElements = partTable.getElementsByTagName('a');
    let urls = [];
    for (let i = 0; i < linkElements.length; i++) {
        urls.push(linkElements[i].href);
    }

    if (urls.length != 0) {

        var fileNames = [];
        for (let i = 0; i < linkElements.length; i++) {
            fileNames.push(linkElements[i].innerText);
        }

        $.ajax({
            type: "POST",
            url: "/WebOrder/DownloadPartFiles/",
            data: { 'fileLocations': urls, 'fileNames': fileNames },
            success: function (response) {
                var blob = new Blob([response], { type: "application/zip" });
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "PartFiles.zip";
                link.click();
                window.URL.revokeObjectURL(blob);
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
    }
}

Now the issue I keep running into is that I can't open the zip folder within Windows 10. Every time I try to open the zip folder using Windows or 7-zip I get an error message that the folder can't be opened or the folder is invalid. I've tried looking at various similar issues on stackoverflow, ie Invalid zip file after creating it with System.IO.Compression, but still can't figure out why this is.

Could it be the encoding? I found that Ajax expects its responses to be encoded UTF-8 and when I view the zip file using notepad++ with UTF-8 I see that there are � characters indicating corruption.

Any thoughts on this would be helpful. Let me know if more information is needed.

If one of the corrupt zip files is needed I can provide that as well.

Edit:

I have since changed my method of receiving the byte array in javascript. I am using a XMLHttpRequest to receive the byte array.

        var parameters = {};
        parameters.FileLocations = urls;
        parameters.FileNames = fileNames;

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", "/WebOrder/DownloadPartFiles/", true);
        xmlhttp.setRequestHeader("Content-Type", "application/json");
        xmlhttp.responseType = "arraybuffer";

        xmlhttp.onload = function (oEvent) {
            var arrayBuffer = xmlhttp.response;
            if (arrayBuffer) {
                var byteArray = new Uint8Array(arrayBuffer);
                var blob = new Blob([byteArray], { type: "application/zip" });
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "PartFiles.zip";
                link.click();
                window.URL.revokeObjectURL(blob);
            }
        }

        xmlhttp.send(JSON.stringify(parameters));

From what I read, Ajax is not the best for receiving byte arrays and binary data. With this method I was able to open one of the zip file with 7-zip, but not Windows, however, one of the files within the archive was showing as a size of 0KB and couldn't be opened. The other three files in the archive were fine. Other zip folders with different files could not be opened at all though.


Solution

  • After some time I found a post that was able to fix my issue, Create zip file from byte[]

    From that post this is the revised method I'm using to create a zip folder with files in it.

            public IActionResult DownloadPartFiles([FromBody] FileRequestParameters parameters)
            {
                List<InMemoryFile> files = new List<InMemoryFile>();
                for (int i = 0; i < parameters.FileNames.Length; i++)
                {
                    InMemoryFile inMemoryFile = GetInMemoryFile(parameters.FileLocations[i], parameters.FileNames[i]).Result;
                    files.Add(inMemoryFile);
                }
                byte[] archiveFile = null;
                using (MemoryStream archiveStream = new MemoryStream())
                {
                    using (ZipArchive archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
                    {
                        foreach (InMemoryFile file in files)
                        {
                            ZipArchiveEntry zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Optimal);
                            using (MemoryStream originalFileStream = new MemoryStream(file.Content))
                            using (Stream zipStream = zipArchiveEntry.Open())
                            {
                                originalFileStream.CopyTo(zipStream);
                            }
                        }
                    }
                    archiveFile = archiveStream.ToArray();
                }
                return File(archiveFile, "application/octet-stream");
            }
    

    I still don't know why the previous method was having issues so if anyone knows the answer to that in the future I'd love to know.