0

I have no idea why this won't work - starting to drive me crazy!

Nobody else seems to have this problem

My code looks something like this:

        Stream stream = new MemoryStream();
        try
        {
            // output zip
            using (ZipFile zipFile = new ZipFile())
            {
                zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                ForEach(DataFile file In DataFiles)
                {
                    String sourcePath = dataFile.Path;
                    String archiveName = dataFile.RealName;
                    //byte[] fileContents = File.ReadAllBytes(sourcePath);
                    //zipFile.AddEntry(archiveName, fileContents);
                    zipFile.AddFile(sourcePath, archiveName);
                    //e.FileName = archiveName;
                }

                zipFile.AddEntry("README.TXT", readmeMessage);
                zipFile.Save(stream);
            }
        }
        catch (Exception ex)
        {
            throw;
        }

All the file paths are valid.

Adding the readme file works fine, if I comment out the rest. But adding the other files causes the zip file to be invalid.

I would appreciate any suggestions!

Thanks

UPDATE

I've noticed that if I initialise the stream with exactly the right size it works. Does anyone know how to get the size of the archive without writing it to disk?

FURTHER UPDATE

I've added a workaround for now.

I can do what I need to by saving the archive to disk then reading it back into another stream

Very lame and I really don't like it, so if anyone can suggest a better way I'd be grateful!

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97

2 Answers2

1

I've never worked with it, but looking into the examples (some checking, comments and no relevant lines removed) of the DotNetZip Library I found this:

public static void Main(String[] args)
{
    ... some input checking ...

    string ZipFileToCreate = args[0];
    string DirectoryToZip = args[1];
    try
    {
        using (ZipFile zip = new ZipFile())
        {
            // note: this does not recurse directories! 
            String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip);
            foreach (String filename in filenames)
            {
                Console.WriteLine("Adding {0}...", filename);
                ZipEntry e = zip.AddFile(filename);
                e.Comment = "Added by Cheeso's CreateZip utility.";
            }

            zip.Save(ZipFileToCreate);
        }
    }
    catch (System.Exception ex1)
    {
        System.Console.Error.WriteLine("exception: " + ex1);
    }
}
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
Christian St.
  • 1,751
  • 2
  • 22
  • 41
  • Strangely if I save to a file it works fine! Saving to a stream gives me problems. +1 for giving me that clue! – CompanyDroneFromSector7G Aug 17 '13 at 22:48
  • Have you read the given examples from the library? Look into the ZlibStreamExample and show us code and error messages, you've tried. – Christian St. Aug 18 '13 at 10:50
  • Yes I have. The code is in the question. Which particular example did you have in mind? There aren't that many for saving to a stream. – CompanyDroneFromSector7G Aug 18 '13 at 11:58
  • @bukko The one I mentioned: ZlibStreamExample, try it out. How did you initialize your `stream` variable? – Christian St. Aug 19 '13 at 11:44
  • I added the declaration for `stream`. I can't find that example - can you give the url? Note that I'm using the zip library not zlib as I need PK compatible zip32 compression; is zlib compatible with zip? Thanks – CompanyDroneFromSector7G Aug 20 '13 at 10:56
  • @bukko Download the package [here](http://dotnetzip.codeplex.com/) and browse to Examples/C#/ZLIB/ZlibStreamExample.cs. Zlib is a software library and uses the DEFLATE algorithm, which is the same for zip archives. – Christian St. Aug 20 '13 at 20:43
0

It seems that once the zip archive reaches a certain size (I haven't tested enough to be sure of the actual limit) it cannot be reliably saved to a stream.

Although nobody seems to be saying it, the only way around this is to save the archive to disk.

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97