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;
}
4

1 回答 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;
}
于 2010-06-04T02:17:56.103 回答