sharpziplib +提取单个文件

时间:2010-06-04 02:10:44

标签: 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;
}

1 个答案:

答案 0 :(得分: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;
}