Skip to content

CopyToAsync fails with ZipOutputStream #167

@vector-man

Description

@vector-man

Steps to reproduce

An exception occurs when adding a file if using CopyToAsync

I'm using Windows 10 x64.

    private const int BufferSize = 4096;

    private async Task CompressFolderAsync(string path, ZipOutputStream zipStream, int folderOffset, CancellationToken cancellationToken)
    {
        string[] files = Directory.GetFiles(path);

        foreach (string filename in files)
        {
            cancellationToken.ThrowIfCancellationRequested();

            FileInfo fi = new FileInfo(filename);

            string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
            entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

            // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
            // A password on the ZipOutputStream is required if using AES.
            //   newEntry.AESKeySize = 256;

            // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
            // you need to do one of the following: Specify UseZip64.Off, or set the Size.
            // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
            // but the zip will be in Zip64 format which not all utilities can understand.
            //   zipStream.UseZip64 = UseZip64.Off;
            newEntry.Size = fi.Length;

            zipStream.PutNextEntry(newEntry);

            // Zip the file.
            // the "using" will close the stream even if an exception occurs
            using (FileStream streamReader = File.OpenRead(filename))
            {
                await streamReader.CopyToAsync(zipStream, BufferSize); // <- This throws an exception.
               // If I use streamReader.CopyTo(zipStream, BufferSize), no error occurs. 
            }
            zipStream.CloseEntry();
        }
        string[] folders = Directory.GetDirectories(path);
        foreach (string folder in folders)
        {
            await CompressFolderAsync(folder, zipStream, folderOffset, cancellationToken);
        }
    }

Expected behavior

The file should be copied without error.

Actual behavior

I get System.NotSupportedException with this message: Size was 0, but I expected y"

Version of SharpZipLib

0.86.0

Obtained from (place an x between the brackets for all that apply)

  • Compiled from source
  • branch: _______
  • commit: _______
  • Downloaded DLL from GitHub
  • Downloaded DLL from SourceForge
  • Downloaded DLL from _______
  • DLL included as part of
  • Package installed using:
  • NuGet
  • MyGet
  • Chocolatey

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions