Exemplo n.º 1
0
// TODO: using this for XPS files results in documents that Microsoft XPS Viewer can't read
IStream *OpenDirAsZipStream(const WCHAR *dirPath, bool recursive)
{
    ScopedComPtr<IStream> stream;
    if (FAILED(CreateStreamOnHGlobal(NULL, TRUE, &stream)))
        return NULL;

    zlib_filefunc64_def ffunc;
    fill_win32s_filefunc64(&ffunc);
    zipFile zf = zipOpen2_64(stream, 0, NULL, &ffunc);
    if (!zf)
        return NULL;

    DirIter iter;
    if (!iter.Start(dirPath, recursive))
        return NULL;

    size_t dirLen = str::Len(dirPath);
    if (!path::IsSep(dirPath[dirLen - 1]))
        dirLen++;

    bool ok = true;
    const WCHAR *filePath;
    while (ok && (filePath = iter.Next()) != NULL) {
        CrashIf(!str::StartsWith(filePath, dirPath));
        const WCHAR *nameInZip = filePath + dirLen;
        size_t fileSize;
        ScopedMem<char> fileData(file::ReadAll(filePath, &fileSize));
        ok = fileData && AppendFileToZip(zf, nameInZip, fileData, fileSize);
    }
    int err = zipClose(zf, NULL);
    if (!ok || err != ZIP_OK)
        return NULL;

    stream->AddRef();
    return stream;
}