CompressedSize in local header does not match that of central directory header in new zip file

I have a strange problem when creating a new Zip archive using SharpZipLib. I can create the Zip file successfully and it can be read fine by various Zip utilities, but when extracting the file contained within, I get an error resembling the following:

  • 'The compressed size stored in the local header for this file is not the same as the compressed size stored in the central directory header.'

Using a Hex editor, I can see that indeed the Central directory header has a compressed size of 64 bytes (correct) for my file, but the local header is FFFF which is incorrect. I have debugged my code and have found that the CompressedSize of the ZipEntry object gets set when you call ZipOutputStream.Finish() but the data in the stream does not get updated with this value - presumably because it was written to the stream before the compressedsize was available. Short of manually searching the stream and updating the value with the value ripped from the Central directory header, which is a bit hacky, I'm out of ideas. If anyone can spot anything obvious I'm doing wrong, it would be very much appreciated.

My code is as follows:

        // get the data and store in unzipped byte array
        Byte[ unzippedData = encoding.GetBytes(data);
        // create CRC for unzipped data
        Crc32 crc = new Crc32();
        crc.Reset();
        crc.Update(unzippedData);

        // create memory stream to hold zipped data
        MemoryStream zippedStream = new MemoryStream();
        ZipOutputStream zipOut = new ZipOutputStream(zippedStream);
       
        ZipEntry zipEntry = new ZipEntry(ConfigurationManager.AppSettings["CSVName"]);
        zipEntry.CompressionMethod = CompressionMethod.Deflated;     
        zipEntry.Size = unzippedData.Length;
        zipEntry.Crc = crc.Value;
        zipEntry.ZipFileIndex = 1;
        zipEntry.DateTime = DateTime.Now;
       
        // write header for our csv file
        zipOut.PutNextEntry(zipEntry);   
        // write the file data
        zipOut.Write(unzippedData, 0, unzippedData.Length);

        // ** ONLY AFTER THIS POINT DOES THE COMPRESSEDSIZE GET SET IN ZIPENTRY
        zipOut.Finish();
        zipOut.Flush();
        // insert the NHS string at beginning of data, before header
        IEnumerable<Byte> zippedData = zippedStream.ToArray();
        IEnumerable<Byte> nhsString = encoding.GetBytes(_NHSZipString);
        zippedData = nhsString.Concat(zippedData);

        // clean up
        zipOut.Close();

        // return as array of bytes
        return zippedData.ToArray();

 Many thanks

 Dan

 

 

 

 

 

 

 

Answer

 

 

Just a quick follow up. This problem seems to be resolved if I don't set the CRC for the ZipEntry - the local header has the expected value and the file extracts successfully in multiple Zip applications. This ring any bells with anyone?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值