Sharpziplib +提取单个文件

发布于 2024-09-03 18:55:38 字数 453 浏览 1 评论 0原文

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

Whenever i try to get the file, the length of the input stream (s.Length) is always zero, what am i doing wrong? ZipEntry is valid and has the proper size of the file, etc.

Here is the code im using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2024-09-10 18:55:38

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

The input stream will not have a length. Use ZipEntry.Size instead.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文