Skip to content

Password-protected ZIP archives cannot be extracted if they contain an empty file #132213

Description

@pawlos

Description

A password-protected archive containing an empty file cannot be read back by the code that wrote it.

Reproduction Steps

using System.IO.Compression;

const string Password = "hunter2";

Directory.CreateDirectory("src");
File.WriteAllText("src/readme.txt", "hello world");
File.WriteAllText("src/.gitkeep", "");              // ordinary empty file

ZipFile.CreateFromDirectory("src", "out.zip", new ZipFileCreationOptions
{
    Password = Password.AsMemory(),
    EncryptionMethod = ZipEncryptionMethod.Aes256,
});

ZipFile.ExtractToDirectory("out.zip", "dst", new ZipExtractionOptions
{
    Password = Password.AsMemory(),
});   // throws InvalidDataException

Expected behavior

An archive written by System.IO.Compression can be read back by System.IO.Compression, and every entry created with a password is encrypted with the requested method regardless of its length.

Actual behavior

Unhandled exception. System.IO.InvalidDataException: The archive entry was compressed using an unsupported compression method.
   at System.IO.Compression.ZipArchiveEntry.OpenCore(FileAccess access, ReadOnlySpan`1 password)
   at System.IO.Compression.ZipFileExtensions.ExtractToFile(ZipArchiveEntry source, String destinationFileName, Boolean overwrite, ReadOnlySpan`1 password)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding, Boolean overwriteFiles, ReadOnlySpan`1 password)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, ZipExtractionOptions options)

Regression?

No. The password support is new in .NET 11 Preview 7, so this has not worked previously.

Known Workarounds

None.

Configuration

Tested on:
.NET SDK 11.0.100-preview.7.26381.103,
runtime 11.0.0-preview.7.26381.103,
linux-x64,
window-x64

Other information

Total Commander, see this empty entry as un-ecrypted and can read w/o issue. 7-zip also sees as un-ecrypted but generates an error on read.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions