I am using Aspose.Zip to create Zip files. However, for many of them (but not all), I get an error message when opening the Zip files in Windows. “Windows cannot open the folder.”
The Zip files can be opened via code. They can also be opened via 7zip. But not Windows. And my users don’t generally have 7zip. We’ve tried multiple versions of Windows but none seem able to open it.
When compared to a Zip file of the same source files created via Windows, the Aspose.Zip file is significantly larger. The source files are PDFs and I’ve validated them and they appear to be valid.
Here are my test files. The problem occurs with many files of different types, numbers, and sources. But these are some of my sample files I was able to recreate the problem with. Specifically this one: Public (Staff).pdf (85.2 KB)
Here is my code. It’s very simple. All of it is in memory as this is a web app of mine. (Though this code actually comes from a MS Visual Studio Unit Test project.)
using (var ms = new System.IO.MemoryStream())
{
using (var archive = new Aspose.Zip.Archive())
{
foreach (var item in all)
{
var fileStream = new System.IO.MemoryStream(item.Data);
archive.CreateEntry(item.Name, fileStream);
}
archive.Save(ms);
}
}
“All” in this case is just an array of these PDFs with their bytes and names. It’s this type and the bytes are accurate:
internal class fileInfo
{
public string Name { get; set; }
public byte[] Data { get; set; }
}
Here’s the Zip created via Windows. It works. It contains all the PDFs. (It’s smaller than the failing ones that Aspose.Zip creates, too.) Windows.zip (565.3 KB)
@jsierks
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): ZIPNET-1047
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Hello @jsierks, I’ve played with your data files and was unable to reproduce this bug. Upon investigation I have an idea why it may happens.
Archive “Fails - Aspose.Zip.zip” you provided has 1 054 080 bytes. Using Aspose.ZIp I’ve compressed the same PDF documents constituting erroneous archive and got 578 854 bytes - nearly the same size that “Widnows.zip” has. The difference is less than 100 bytes.
So, my hypothesis is that you flush the result to disk so it may partly override existing archive and mix bytes of two correct archives resulting “semi-correct” zip file. How do you flush ms in your code?
To get my idea please review following code
byte[] aaa = new byte[] { 97, 97, 97 };
using (FileStream fs = new FileStream("r.bin", FileMode.OpenOrCreate))
fs.Write(aaa, 0, aaa.Length);
byte[] bb = new byte[] { 98, 98 };
using (FileStream fs = new FileStream("r.bin", FileMode.OpenOrCreate))
fs.Write(bb, 0, bb.Length);
What would be content and length of r.bin after execution? It would be bba, 3 bytes. Third a sits there since first writing, bb overrides first and second symbols at second writing.
Does not something similar happen at your case?
My code is above. As you can see I am not calling Flush for the MemoryStream. Flush is not needed for MemoryStream - it doesn’t do anything. (Though I have tested with it and the problem is still there.)
I have a web app. I cannot use a FileStream. Did you test with a MemoryStream? I provided the code to test it. The only thing missing is where I write all those bytes to file, like so:
System.IO.File.WriteAllBytes(“Test.zip”, ms.GetBuffer());
All of the invalid Zips that I attached were created with this code. And I agree - they are incorrect. But they do still open in 7zip. And the files aren’t duplicated or anything. The issue is getting them to open in Windows. Also, the bytes appear to be incorrect in code, so the error does not appear to be with the WriteAllBytes. Which is why I opened this ticket, as I believe the issue is with Aspose.Zip and how it is writing to the MemoryStream.