简体   繁体   中英

zip and unzip files using DotNetZip

I am using DotNetZip. Using it to zip mp3 files.

ZipFile zip = new ZipFile();
zip.Password = "123";
zip.AddFile("R:\\abc\\a\\a 2 z.mp3");
zip.Save("R:\\abc\\a\\aaa.zip");

After extraction of aaa.zip, I get a corrupted mp3 file. Having 3.31MB data when original had 3.62MB. How to resolve this problem? Any help is appreciated.

The documentation states here :

Be aware that the ZipFile class implements the IDisposable interface. In order for ZipFile to produce a valid zip file, you use use it within a using clause (Using in VB), or call the Dispose() method explicitly. See the examples for how to employ a using clause.

So try to wrap your code in a using block:

using (ZipFile zip = new ZipFile())
{
   zip.Password = "123";
   zip.AddFile("R:\\abc\\a\\a 2 z.mp3");
   zip.Save("R:\\abc\\a\\aaa.zip");
}

Also refer to the various example on Save documentation page.

The problem is really unknown. The problem happens for only that specified file. I've tried with other files and found no problems. Thank you guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM