How to Compress and Decompress Files with DotNetZip

Troubleshooting Common DotNetZip Errors and Fixes

DotNetZip is a popular .NET library for creating and reading ZIP archives. Below are common errors developers encounter with DotNetZip and concise fixes you can apply.

1. ZipFile throws “Archive already open” or “Zip file is corrupt”

  • Cause: Attempting to open the same ZIP stream multiple times or previous write left archive in an inconsistent state.
  • Fixes:
    • Ensure each ZipFile instance is opened once. Use a single using block:

      Code

      using (var zip = ZipFile.Read(path)) { /… / }
    • If using streams, reset position before reusing:

      Code

      stream.Position = 0; using (var zip = ZipFile.Read(stream)) { // }
    • If corruption persists, try repairing by extracting valid entries then creating a new archive.

2. “The process cannot access the file because it is being used by another process”

  • Cause: File handle still open (e.g., not disposed) or antivirus/backup locking the file.
  • Fixes:
    • Always dispose streams and ZipFile objects promptly with using blocks.
    • Close any FileStream before writing the ZIP:

      Code

      using (var fs = File.OpenRead(filePath)) { / read */ } // then create zip
    • Retry with a short backoff when external processes may lock the file.

3. Entries missing after Save()

  • Cause: Adding entries from streams whose positions are at the end, or not adding entries correctly.
  • Fixes:
    • Reset stream.Position = 0 before adding:

      Code

      stream.Position = 0; zip.AddEntry(“name”, stream);
    • Use AddFile/AddEntry correctly; confirm Save is called and completed.
    • If adding large files, consider SaveProgress event to verify progress and check for exceptions.

4. Incorrect file paths or directory structure inside ZIP

  • Cause: Using full file paths when adding files, causing unintended folder structure.
  • Fixes:
    • Use AddFile(path, directoryPathInArchive):

      Code

      zip.AddFile(filePath, “folder-inside-zip”);
    • For flat structure, use Path.GetFileName:

      Code

      zip.AddEntry(Path.GetFileName(filePath), File.ReadAllBytes(filePath));

5. Password-protected entries fail to extract

  • Cause: Wrong password, or using the wrong encryption algorithm.
  • Fixes:
    • Set Password before extraction:

      Code

      zip.Password = “secret”; zip.ExtractAll(dest);
    • Ensure entries were created with compatible encryption (DotNetZip supports ZipCrypto and AES). If archive uses unsupported encryption, use a compatible tool to re-create.

6. Memory usage spikes with large archives

  • Cause: Loading entire archive or large files into memory.
  • Fixes:
    • Use streaming APIs and avoid reading entire files into byte[].
    • Extract entries one at a time instead of calling ExtractAll.
    • For creating large archives, add files from disk (AddFile) rather than AddEntry with full byte arrays.

7. Unexpected character encoding in file names

  • Cause: Zip uses different text encoding for entry names.
  • Fixes:
    • Set ProvisionalAlternateEncoding when reading/writing:

      Code

      zip.AlternateEncoding = Encoding.UTF8; zip.AlternateEncodingUsage = ZipOption.Always;
    • Recreate archive with desired encoding for compatibility.

8. Exceptions when running on non-Windows platforms

  • Cause: File path separators, permissions, or platform-specific behaviors.
  • Fixes:
    • Use Path.Combine and Path.Directory

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *