1

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.

whoone
  • 533
  • 3
  • 7
  • 16
  • Not reproducible. Please provide your code that has this issue. – JustAnotherUser Dec 09 '12 at 14:15
  • Please provide the file which is causing problems – Tomas Grosup Dec 09 '12 at 15:28
  • I don't know why you're encrypting, but if it's to assure that the file cannot be retrieved without the password, then you need to use real encryption. You would need to add after setting the password: `zip.Encryption = EncryptionAlgorithm.WinZipAes256;` The file would then only be extractable by PKZip and WinZip, but it would actually be encrypted. The old PKZip encryption, which is what your code would do, is not really encryption since it is easily breakable. – Mark Adler Dec 09 '12 at 16:33
  • What are you using to extract? – Mark Adler Dec 09 '12 at 16:34
  • You need to provide your _entire_ code, _exactly_ as run, for others to be able to spot your error. – Mark Adler Dec 09 '12 at 16:36
  • Note that you will not get any compression on `.mp3` files. They are already compressed. You can use zip simply for packaging the files. If you really are only zipping `.mp3` files, then use: `zip.CompressionLevel = Ionic.Zlib.CompressionLevel.None;` to turn off compression. This will make the zipping go much faster since it doesn't try to compress, and will produce the same result. – Mark Adler Dec 09 '12 at 16:39

2 Answers2

2

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.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • That's too bad, but since this is what the developers of the library suggest is the "right way to do it", I would stick with it anyway (while looking for other options/reasons). YMMV. – Christian.K Dec 09 '12 at 14:27
  • You might also want to try, just for good measure, adding some other file (e.g. a small text file or so) to the archive and see if that works out correctly. – Christian.K Dec 09 '12 at 14:28
  • Once you exit the program, there is no difference between using `using` and not using `using`. Hence the possible no change. – Mark Adler Dec 09 '12 at 16:35
  • 1
    @MarkAdler That is not necessarily true. At the end, the OS will close the open file handles, but it will not take care of proper finishing pending writing to open files. Besides, the CLR will not call outstanding `Dispose` methods for you. It doesn't even know about that. – Christian.K Dec 09 '12 at 16:52
  • 1
    Maybe not. Though the DotNetZip documentation indicates that all that its `Dispose` method does is close the file pointer. It also indicates through a negative implication that the zip file will be usable after the application exits if `using` is not used. – Mark Adler Dec 09 '12 at 17:07
0

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.

whoone
  • 533
  • 3
  • 7
  • 16