SharpZipLib icon indicating copy to clipboard operation
SharpZipLib copied to clipboard

ZipFile.Create(archivePath) creates 0 bytes archive.

Open AntonMikheev666 opened this issue 6 years ago • 2 comments

Steps to reproduce

  1. Use ZipFile.Create(archivePath) to create an archive.
  2. Close created ZipFile without adding anything to it.

Expected behavior

Created archive have non-zero size (should have "End of central directory record" and be at least 22 bytes according to specification)

Actual behavior

Archive size is 0 bytes, attempt to open it with new ZipFile(archivePath) leads to ZipException "Cannot find central directory"

Version of SharpZipLib

1.1.0 also tried 1.2.0

Obtained from (only keep the relevant lines)

  • Package installed using NuGet

AntonMikheev666 avatar Nov 14 '19 10:11 AntonMikheev666

This could be fixed by simply calling ZipFile.BeginUpdate(); and ZipFile.CommitUpdate(); in ZipFile.Create() if the file didn't already exist (or if it's size is 0).

piksel avatar Dec 30 '19 23:12 piksel

The following code could achieve the expected result.

class CreateZipFile
{
	public static void Main(string[] args)
	{
		using (ZipFile zip = ZipFile.Create(@".\test.zip"))
		{
			zip.BeginUpdate();			
			zip.CommitUpdate();
		}
	}
}

If 1.Use ZipFile.Create(archivePath) to create an archive. 2.Close created ZipFile without adding anything to it, the generated zip is decompressed by 7zip or WinRAR will prompt The archive is either in unknown format or damaged.

willson-chen avatar Jun 09 '20 09:06 willson-chen