sharpziplib +提取单个文件

sch*_*opy 3 c# sharpziplib

每当我尝试获取文件时,输入流的长度(s.Length)总是为零,我做错了什么?ZipEntry是有效的,具有适当的文件大小等.

这是我使用的代码:

public static byte[] GetFileFromZip(string zipPath, string fileName)
{
    byte[] ret = null;
    ZipFile zf = new ZipFile(zipPath);
    ZipEntry ze = zf.GetEntry(fileName);

    if (ze != null)
    {
        Stream s = zf.GetInputStream(ze);
        ret = new byte[s.Length];
        s.Read(ret, 0, ret.Length);
    }

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*eff 10

输入流不会有长度.请ZipEntry.Size改用.

public static byte[] GetFileFromZip(string zipPath, string fileName)
{
    byte[] ret = null;
    ZipFile zf = new ZipFile(zipPath);
    ZipEntry ze = zf.GetEntry(fileName);

    if (ze != null)
    {
        Stream s = zf.GetInputStream(ze);
        ret = new byte[ze.Size];
        s.Read(ret, 0, ret.Length);
    }

    return ret;
}
Run Code Online (Sandbox Code Playgroud)