I'm trying to compress 2 pdf files using DotNetZip with this code:
void Main()
{
byte[] file1 = File.ReadAllBytes(@"C:\temp\a.pdf");
byte[] file2 = File.ReadAllBytes(@"C:\temp\b.pdf");
//byte[] file3 = File.ReadAllBytes(@"C:\temp\c.pdf");
byte[] zip = Zipper.CreateZipFromFileContentList(new List<Tuple<string, byte[]>> {
new Tuple<string, byte[]>(@"a.pdf", file1),
new Tuple<string, byte[]>(@"b.pdf", file2)//,
//new Tuple<string, byte[]>(@"c.pdf", file3)
});
File.WriteAllBytes(@"C:\temp\c.zip", zip);
using(Ionic.Zip.ZipFile zipFile = Ionic.Zip.ZipFile.Read(@"C:\temp\c.zip"))
{
foreach(ZipEntry entry in zipFile)
{
entry.Extract(@"C:\temp\t");
}
}
}
// Define other methods and classes here
static class Zipper
{
public static byte[] CreateZipFromFileContentList(IList<Tuple<string, byte[]>> fileContentList)
{
try
{
using (ZipFile zip = new ZipFile())
{
int i = 0;
foreach (var item in fileContentList)
{
ZipEntry entry = null;
entry = zip.AddEntry(item.Item1, item.Item2);
++i;
}
MemoryStream ms = new MemoryStream();
zip.Save(ms);
return ms.GetBuffer();
}
}
catch (Exception)
{
throw;
}
}
}
Actually everything works, because the extract process works after the archive is created. But if I try to open the zip file using winRar, I get a message that the archive is damaged. If I try with 7Zip, I get a message saying that there are data beyond the end of the useful block (translated, I don't know if the english version gives the exact same message).
If I zip 1 or 3 files, I have no problems at all. How can I fix that?
Your zip output probably has extra bytes because .GetBuffer()
returns the streams internal buffer - only part of which contains valid data.
Use .ToArray()
instead which will return only the used part of the buffer.