CheckZip fails always

Oct 6, 2011 at 4:03 PM

Hi,

if I zip an entire directory with AddDirectory the CheckZip method always returns false (that means failed), although I can open it and all files are there.

If I check the zip file with 7-Zip it says that the archive has nor errors, too.

Can anybody help me?

Coordinator
Oct 6, 2011 at 6:59 PM
Edited Oct 6, 2011 at 7:00 PM

show your code.

 how are you calling CheckZip ?

Oct 7, 2011 at 8:52 AM
Edited Oct 7, 2011 at 3:48 PM

Hi, I don't know why, but now it works. By the way, what is the difference between IsZipFile and CheckZip?

ZipFile^ zip;
	try
	{	
		String^ dirPathDotNet = gcnew String(dirPath.c_str());
		zip = gcnew ZipFile();
		zip->SaveProgress += gcnew EventHandler<SaveProgressEventArgs^>(&DotNetZipLibWrapper::SaveProgress);
		zip->Comment = "test comment";
		zip->CompressionMethod = CompressionMethod::None;		
		zip->AddDirectory(dirPathDotNet);		
		zip->Save(dirPathDotNet + ".zip");			
		return ZipFile::CheckZip(dirPathDotNet + ".zip"); //this returns always false
	}
	catch(ZipException^ ex){
		Windows::Forms::MessageBox::Show("Zipping failed! " + ex->Message);		
		return false;
	}
	finally
	{
		delete zip;
	}
Coordinator
Oct 8, 2011 at 2:16 AM

on the difference between those methods, check the doc.  IsZipFile is a more cursory check, it looks for a signature. CheckZip can actually do a full exctraction of the zipfile, which fully verifies the archive. But read the documentation to get the full story.

I'm glad your code is working now.  But I wouldn't count on it, the way you have it written.  Normally you will have to delete the ZipFile instance before attempting to read it again. The delete will cause the finalizer/dispose to be invoked, which closes all open files and releases them, which makes the zip file readable again.  If you call CheckZip on the zipfile before calling dispose on the instance used to write the file, it's possible that the file is still open, in which case the CheckZip will fail.

Consider relocating the CheckZip to after the finally clause.

 

Oct 10, 2011 at 10:41 AM

Thanks for the hint.

But I have 2 other question:

Is it possible to prevent opening a zip file so you cannot see the entries?

Provides DNZ a way to create a checksum from the hole zip archiv?

Coordinator
Oct 10, 2011 at 7:38 PM

As for "preventing seeing the entries" of a zip file ...one way to do it is to double-wrap your entries.  if you protect a zip entry with a password, when you read the zip, you can read the entry name, size, timestamp, and other metadata, but you cannot read the content.  If you store a zip into a zip, and protect the inner zip with a password, then a reader of the outer zip will see the name, size and timestamp of the inner zip, but not the contents of the inner zip.

About the checksum - you could layer a checksum on top of the zip, either by double-wrapping, as I described above, or via a do-it-yourself approach. The double-wrapping works because each entry in a zip archive is checksummed.  If you store a zip into a zip, then the inner zip is checksummed upon extraction.  You could do-it-yourself by creating a simple zip, then computing the checksum, and storing each (the zip and the checksum) independently.  This is a typical way people provide security checks for downloads: they publish the file and the MD5 or SHA1 hash of the file.