Could not read signature - no data! (position 0x00005601)

Apr 28, 2009 at 9:07 PM
Edited Apr 28, 2009 at 9:12 PM
Here's my problem,

I created a static class (ZipUtil.cs) which has a bunch of methods which call DotNetLib and create an archive, update an archive, delete a file in the archive etc.
The archive that is created when I call the static class throws the error "Could not read signature - no data!  (position 0x00005601)" when I tried to add a file using the static class.
But when I take the same code that is in the static class and put it directly in my main method (the commented stuff in Program.cs), the archive created works just fine when I try to add a file using the static class.

Here are my two files.

Program.cs


using System;
using Cps.Utility.CompressionUtils;
using Ionic.Zip;

namespace Cps.Utility
{
class Program
{
static void Main(string[] args)
{
//string dirname = @"c:\tmp\dir1";
//string dir1 = "dir1";
//using (ZipFile zip = new ZipFile(dirname + ".zip"))
//{
// //zip.AddDirectory(@"c:\tmp\dir1", "dir1");
// zip.AddDirectory(dirname, dir1);
// zip.Save();
//}
if ("create".Equals(args[0], StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("Extracting Archive");
ZipUtil.createArchive(@"c:\tmp\dir1");
return;
}
if ("extract".Equals(args[0], StringComparison.CurrentCultureIgnoreCase))
{
ZipUtil.extractArchive(@"c:\tmp\dir1.zip", @"c:\tmp\dotnetzip\");
return;
}
if ("add".Equals(args[0], StringComparison.CurrentCultureIgnoreCase))
{
ZipUtil.addFileToArchive("c:\\tmp\\crap.txt", "dir1\\dir12", "c:\\tmp\\dir1.zip");
return;
}
if ("delete".Equals(args[0], StringComparison.CurrentCultureIgnoreCase))
{
ZipUtil.removeFileFromArchive(@"dir\dir12\crap.txt", @"c:\tmp\dir1.zip");
return;
}
if ("replace".Equals(args[0], StringComparison.CurrentCultureIgnoreCase))
{
ZipUtil.updateFileInArchive(args[1], args[2], args[3]);
return;
}
if ("move".Equals(args[0], StringComparison.CurrentCultureIgnoreCase))
{
ZipUtil.moveFileInArchive(args[1], args[2], args[3], args[4]);
return;
}
throw new NotImplementedException();
}
}
}

ZipUtil.cs

using System;
using System.IO;
using Ionic.Zip;

namespace Cps.Utility.CompressionUtils
{
public class ZipUtil
{
public static void createArchive(string directoryPath)
{
if (Directory.Exists(directoryPath) == false)
throw new DirectoryNotFoundException();

using(ZipFile zip = new ZipFile(directoryPath+".zip"))
{
zip.AddDirectory(directoryPath, (new DirectoryInfo(directoryPath)).Name);
zip.Comment = "This zip was created at " + DateTime.Now.ToString("G") ;
zip.Save();
}
}

public static void addFileToArchive(string filePath, string relativePathInArchive, string zipArchivePath)
{
if (File.Exists(zipArchivePath) == false)
throw new FileNotFoundException("File Not Found", zipArchivePath);
//if (ZipFile.IsZipFile(zipArchivePath) == false)
// throw new FileLoadException("Specified archive not a zip file");
if (File.Exists(filePath) == false)
throw new FileNotFoundException("File Not Found", filePath);

using (ZipFile zip = ZipFile.Read(zipArchivePath))
{
zip.AddFile(filePath, relativePathInArchive);
zip.Comment = "This zip archive was updated " + DateTime.Now.ToString("G");
zip.Save(zipArchivePath);
}
}

public static void removeFileFromArchive(string relativePathInArchive, string zipArchivePath)
{
if (File.Exists(zipArchivePath) == false)
throw new FileNotFoundException("File Not Found", zipArchivePath);
if (ZipFile.IsZipFile(zipArchivePath) == false)
throw new FileLoadException("Specified archive not a zip file");

using (ZipFile zip = ZipFile.Read(zipArchivePath))
{
ZipEntry e = zip[relativePathInArchive];

if (e == null)
throw new FileNotFoundException("File not found in archive", relativePathInArchive);

zip.RemoveEntry(relativePathInArchive);
zip.Comment = "This zip archive was updated " + DateTime.Now.ToString("G");
zip.Save();
}
}

public static void updateFileInArchive(string filePath, string relativePathInArchive, string zipArchivePath)
{
if (File.Exists(zipArchivePath) == false)
throw new FileNotFoundException("File Not Found", zipArchivePath);
if (ZipFile.IsZipFile(zipArchivePath) == false)
throw new FileLoadException("Specified archive not a zip file");
if (File.Exists(filePath) == false)
throw new FileNotFoundException("File Not Found", filePath);

using (ZipFile zip = ZipFile.Read(zipArchivePath))
{
// for this entry, update the archive with content from the filesystem
ZipEntry e = zip[relativePathInArchive];

if (e == null)
throw new FileNotFoundException("File not found in archive", relativePathInArchive);

zip.UpdateItem(filePath);
zip.Comment = "This zip archive was updated " + DateTime.Now.ToString("G");
zip.Save();
}
}

public static void moveFileInArchive(string fileName, string filePathInArchive, string destFolderInArchive,
string zipArchivePath)
{
if (File.Exists(zipArchivePath) == false)
throw new FileNotFoundException("File Not Found", zipArchivePath);
if (ZipFile.IsZipFile(zipArchivePath) == false)
throw new FileLoadException("Specified archive not a zip file");

using (ZipFile zip = ZipFile.Read(zipArchivePath))
{
ZipEntry e = zip[filePathInArchive + fileName];

if (e == null)
throw new FileNotFoundException("File not found in archive", filePathInArchive);

e.FileName = destFolderInArchive + filePathInArchive;
zip.Comment = "This zip archive was updated " + DateTime.Now.ToString("G");
zip.Save();
}
}

public static void extractArchive(string zipArchivePath, string destinationFolder)
{
if (File.Exists(zipArchivePath) == false)
throw new FileNotFoundException("File Not Found", zipArchivePath);
if (ZipFile.IsZipFile(zipArchivePath) == false)
throw new FileLoadException("Specified archive not a zip file");

using (ZipFile zip = ZipFile.Read(zipArchivePath))
{
foreach (ZipEntry e in zip)
{
e.Extract(destinationFolder, true); // overwrite == true
}
}
}
}
}



        
    
Apr 28, 2009 at 9:17 PM
It just doesn't make sense.

In scenario 1.

I am creating a new ZipFile() inside a static method.

In the second case, I am calling new ZipFile() from the Main method which is also a static method. Why should the archive created in the first scenario be unrecognizable as a ZipFile (ZipFile.IsZipFile() returns false in scenario 1 but true in scenario 2)

Coordinator
Apr 28, 2009 at 10:02 PM
I don't know what the problem might be, but I think you're on the right track. If the code works for you in one environment, you should be able to migrate it piecewise to the other environment.

Apr 29, 2009 at 1:22 PM
Cheeso,

           It's all on one platform. I am using Windows vista and Visual Studio 2008.

When I create the archive directly, there's no problem.

When I put the creation of the archive into a static method in a utility class and call it, it creates a zip file that is unusable. I don't know how this is even possible. The code is included in the first entry. Please try it and see if you can reproduce the problem.

All you would need apart from the two files is the following.

directories
c:\tmp\dir1
c:\tmp\dir1\dir2

files
c:\tmp\crap.txt

Thanks for your time!!
Coordinator
Apr 29, 2009 at 5:53 PM
Hey, I looked into this further and it looks like it may be a bug in DotNetZip to me.
I will have to look more deeply into it, but it appears to be related to the comment on the archive.
If I remove the line that sets the comment on the archive as it is created, then the archive can later be read successfully.

I will investigate further, and get back to you.




Coordinator
Apr 29, 2009 at 6:13 PM
The problem you saw is a bug in DotNetZip
http://dotnetzip.codeplex.com/WorkItem/View.aspx?WorkItemId=7711

To workaround it, append 4 spaces to the comment text. 

Apr 29, 2009 at 7:03 PM
Thanks Cheeso!!

      You are a life-saver.
Coordinator
Apr 29, 2009 at 7:18 PM
H-
glad to help. You may want to get v1.7.2.13 - an update for v1.7.  That will fix the problem and eliminate the need for the workaround.