Skip to content

SFTP file stream not working with System.IO.Compression.Zip #1018

Closed
@S-Jindal

Description

I am getting the following error when trying to instantiate a ZipArchive object using Sftp file stream.

message='Exception: System.IO.InvalidDataException: Central Directory corrupt.
[2022-10-21T14:16:36.733Z]  ---> System.IO.IOException: Zip file corrupt: unexpected end of stream reached.
[2022-10-21T14:16:36.734Z]    at System.IO.Compression.ZipHelper.ReadBytes(Stream stream, Byte[] buffer, Int32 bytesToRead)
[2022-10-21T14:16:36.734Z]    at System.IO.Compression.ZipHelper.SeekBackwardsAndRead(Stream stream, Byte[] buffer, Int32& bufferPointer)
[2022-10-21T14:16:36.736Z]    at System.IO.Compression.ZipHelper.SeekBackwardsToSignature(Stream stream, UInt32 signatureToFind, Int32 maxBytesToRead)
[2022-10-21T14:16:36.736Z]    at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
[2022-10-21T14:16:36.737Z]    --- End of inner exception stack trace ---
[2022-10-21T14:16:36.737Z]    at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
[2022-10-21T14:16:36.738Z]    at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding)
[2022-10-21T14:16:36.739Z]    at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)

Looking at the backtrace, looks like when trying to read from the end of file it is getting this error: Zip file corrupt: unexpected end of stream reached.

my code is simple:

using(var remoteStream = sftpclient.OpenRead(filepath)
using(var zip = new ZipArchive(stream: remoteStream,  mode: ZipArchiveMode.Read, leaveOpen: true))
{
...
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions