SharpZipLib + 提取单个文件

3
每当我尝试获取文件时,输入流的长度(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;
}
1个回答

11
输入流没有长度。请使用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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接