Files missing after Save(string fileName):void

Jul 6, 2011 at 8:57 AM

Hi,

I tried to use DotNetZip Library today to create a zip file out of a bunch of files on my hard drive. Apparently not all files are included once I decide to write the zip file to disc.

This is my setup:

public class ZippedFile
{
    public static ZipFile Create(string xmlFilepath)
    {
        var zippedFile = new ZipFile();

        zippedFile.Add(xmlFilepath);

        // Code to get the list with all files to include out of the xml file
        // files is a List<string> with filepath's

        foreach(string file in files)
        {
            zippedFile.Add(file);
        }
        return zippedFile;
    }


public class Test
{

    public Test(string filepath)
    {
        ZipFile myZip = ZippedFile.Create(filepath);
        myZip.Save(@"C:\test.zip");
    }

}

If I set a break point right before it saves the zip file at line myZip.Save(@"C:\test.zip") I see 29 Entries being listed which is the right amount of files. The create zip file however only contains 3 files and an empty folder which should normally contain a bunch of the 29 files.

Any suggestions?

Coordinator
Jul 6, 2011 at 2:32 PM

yes, I have a suggestion. Use the ZipFile class in a using clause.  Check any of the examples shown on the examples page, or any of the example code in the documentation. 

using (var zippedFile = new ZipFile())
{
    zippedFile.AddFile(.....);
    zippedFile.Save("PathToZippedFile.zip");
}

You need to call .Dispose() on the ZipFile instance for it to properly close its destination stream. This is as documented. Employing a using clause calls .Dispose() implicitly, and is the easiest way to do what you need to do.

Jul 7, 2011 at 2:02 PM

Apparently this does not solve my problem. I start thinking that DotNetZip Library has a problem adding files without extensions since all files missing in the created zip files are those which have no file extension.

Coordinator
Jul 7, 2011 at 6:54 PM

I just checked explicitly - and DotNetZip has no problem zipping files with no extension. 

There is something else causing your problem. Are you sure the files are not present?  You wrote:  "The create zip file however only contains 3 files and an empty folder which should normally contain a bunch of the 29 files."  How did you verify this?   Can you check with DotNetZip (ZipFile.Read())?  If you can produce a small application that illustrates the problem - something I can run on my computer - we will be able to diagnose it pretty quickly.

 

Jul 8, 2011 at 6:07 AM

I have solved the problem or at least was able to get it work.

File structure:

C:\test\test.xml
C:\test\subdir\1
C:\test\subdir\2
C:\test\subdir\3
C:\test\subdir\4
C:\test\subdir\5
C:\test\subdir\6

test.xml contains the file path for every other file. Now when I simple do the foreach loop over all path arguments using the simple Add(string path) I end up with just the text.xml file and an empty subdir folder in my zipFile (although debugging showed, that right before .Save() all file entries are in my ZipFile. However not after the file was written to disk. To be able to get all my files in the ZipFile I have to use the .Add(string path, "") version to explicitly tell DotNetZip Library that I want all files in my root document of the zip file. I just assumed that's the default behavior if I don't provide a directoryPathInZip.

Coordinator
Jul 8, 2011 at 3:43 PM

I'm glad you got it working.   Seems like it was a misunderstanding or bad assumption on your part.  Your assumption that entries would appear in the root of the zip archive was incorrect.

Stepping back, I always try to make sure that DotNetZip behaves correctly, as it is documented to behave.  You're reporting that it is not behaving correctly, though you found a way to work around that broken behavior.  If you are interested, I still would like to get a test case from you that reproduces the incorrect behavior.  You mentioned "an empty subdir folder in the zipfile".  If you can provide a small test case that reproduces that particular problem, I would be glad to fix it.