Beispiel #1
0
bool ZipArchiveWriter::Open(const std::string& zipFileName, bool bZipExist)
{
    if (zipFileName.empty())
        return false;

    Close();

    m_errCode = Z_OK;
    m_zipFilePath = zipFileName;

#ifdef USEWIN32IOAPI
    zlib_filefunc64_def ffunc;
    fill_win32_filefunc64W(&ffunc);
    m_hZip = zipOpen2_64(TUtf8Util::UTF8StrToUniStr(m_zipFilePath).c_str(), bZipExist ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE, nullptr, &ffunc);
#else
    m_hZip = zipOpen64(m_zipFilePath.c_str(), bZipExist ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE);
#endif

    if (!m_hZip)
    {
        m_errCode = Z_ZIP_OPEN_ERROR;
        return false;
    }

    return true;
}
Beispiel #2
0
bool zip::ZipArchiveInput::Open( String_t const& archiveName, bool caseSensitive )
{
	m_archiveName = archiveName;
	m_caseSensitive = caseSensitive;

#ifdef USEWIN32IOAPI
	zlib_filefunc64_def ffunc;
#endif

#ifdef USEWIN32IOAPI
#	ifdef SCARAB_WCHAR_MODE
	fill_win32_filefunc64W(&ffunc);
#	else
	fill_win32_filefunc64A(&ffunc);
#	endif
	uf = unzOpen2_64(m_archiveName.c_str(),&ffunc);
#else
	uf = unzOpen64(m_archiveName.c_str());
#endif // USEWIN32IOAPI

	if (uf==NULL)
	{
		m_errorMessage << _T("Can't open ") << m_archiveName << std::endl;
		return false;
	}

	return Index();
}
FileUtils::MzZipCtx * FileUtils::mzOpenOutputFile(std::string path)
{
    MzZipCtx *ctx = new(std::nothrow) MzZipCtx();
    if (!ctx) {
        return nullptr;
    }

#if defined(MINIZIP_WIN32)
    fill_win32_filefunc64W(&ctx->buf.filefunc64);
    ctx->path = utf8::utf8ToUtf16(path);
#elif defined(MINIZIP_ANDROID)
    fill_android_filefunc64(&ctx->buf.filefunc64);
    ctx->path = std::move(path);
#else
    fill_fopen64_filefunc(&ctx->buf.filefunc64);
    ctx->path = std::move(path);
#endif

    fill_buffer_filefunc64(&ctx->zFunc, &ctx->buf);
    ctx->zf = zipOpen2_64(ctx->path.c_str(), 0, nullptr, &ctx->zFunc);
    if (!ctx->zf) {
        free(ctx);
        return nullptr;
    }

    return ctx;
}
ZipCtx * MinizipUtils::open_output_file(std::string path)
{
    ZipCtx *ctx = new(std::nothrow) ZipCtx();
    if (!ctx) {
        return nullptr;
    }

#if defined(MINIZIP_WIN32)
    auto converted = utf8_to_wcs(path);
    if (!converted) {
        delete ctx;
        return nullptr;
    }
    ctx->path = std::move(converted.value());

    fill_win32_filefunc64W(&ctx->buf.filefunc64);
#elif defined(MINIZIP_ANDROID)
    fill_android_filefunc64(&ctx->buf.filefunc64);
    ctx->path = std::move(path);
#else
    fill_fopen64_filefunc(&ctx->buf.filefunc64);
    ctx->path = std::move(path);
#endif

    fill_buffer_filefunc64(&ctx->z_func, &ctx->buf);
    ctx->zf = zipOpen2_64(ctx->path.c_str(), 0, nullptr, &ctx->z_func);
    if (!ctx->zf) {
        delete ctx;
        return nullptr;
    }

    return ctx;
}
Beispiel #5
0
// ////////////////////////////////////////////////////////////////////////////////
// @public @static 将指定文件名打包成指定 zip
//
bool ZipTools::Zip(LPCWSTR wzDirPath, LPCWSTR wzDestName)
{
	//
	// 检查指定路径是否存在
	//
	if ( !FileTools::Exist(wzDirPath) )
	{
		DebugTools::OutputDebugPrintfW(L"[ZipTools] [Zip] Path Not Exist. [%s]\r\n", wzDirPath);
		return false;
	}

	WCHAR wzDestDir[MAX_PATH] = {0};
	FileTools::GetFileDir(wzDestName, wzDestDir, L'\\');

	if ( !FileTools::Exist(wzDestDir) )
	{
		DebugTools::OutputDebugPrintfW(L"[ZipTools] [Zip] Path Not Exist. [%s]\r\n", wzDestDir);
		return false;
	}

	// 设置当前工作目录
	SetCurrentDirectoryW(wzDirPath);

	WCHAR wzParentDir[MAX_PATH] = {0};
	wcscpy_s(wzParentDir, wzDirPath);
	//FileTools::GetFileDir(wzDirPath, wzParentDir, L'\\');
	wcscat_s(wzParentDir, L"\\");

	//
	// 创建 zip 文件
	//
	zlib_filefunc64_def ffunc;
	fill_win32_filefunc64W(&ffunc);
	zipFile zf; zf = zipOpen2_64(wzDestName, 0, NULL, &ffunc);

	if ( NULL == zf )
	{
		DebugTools::OutputDebugPrintfW(
			L"[ZipTools] [Zip] Create Zip File Failed. [%s] \r\n", wzDestName);
		return false;
	}

	unsigned long size_read = 0;
	unsigned long size_buf = WRITEBUFFERSIZE;
	PBYTE buf = new BYTE [size_buf];

	//
	// 将文件夹下所有文件添加进 zip
	//
	AddFileToZip(zf, wzDirPath, wzParentDir, buf);

	zipClose(zf,NULL);

	delete [] buf;

	return true;
}
zipFile FileUtils::mzOpenOutputFile(const std::string &path)
{
#if defined(MINIZIP_WIN32)
    zlib_filefunc64_def zFunc;
    memset(&zFunc, 0, sizeof(zFunc));
    fill_win32_filefunc64W(&zFunc);
    return zipOpen2_64(utf8::utf8ToUtf16(path).c_str(), 0, nullptr, &zFunc);
#elif defined(MINIZIP_ANDROID)
    zlib_filefunc64_def zFunc;
    memset(&zFunc, 0, sizeof(zFunc));
    fill_android_filefunc64(&zFunc);
    return zipOpen2_64(path.c_str(), 0, nullptr, &zFunc);
#else
    return zipOpen64(path.c_str(), 0);
#endif
}
Beispiel #7
0
			static DKObject<UnZipFile> Create(const DKString& zipFile, const DKString& file, const char* password)
			{
				if (zipFile.Length() == 0 || file.Length() == 0)
					return NULL;

				DKString filename = zipFile.FilePathString();

				unzFile uf = NULL;
#ifdef _WIN32
				{
					zlib_filefunc64_def ffunc;
					fill_win32_filefunc64W(&ffunc);
					uf = unzOpen2_64((const wchar_t*)filename, &ffunc); // UTF16LE
				}
#else
				{
					DKStringU8 filenameUTF8(filename);
					if (filenameUTF8.Bytes() > 0)
						uf = unzOpen64((const char*)filenameUTF8); // UTF8
				}
#endif
				if (uf)
				{
					unz_file_info64 file_info;
					DKStringU8 fileUTF8(file);
					if (fileUTF8.Bytes() > 0 &&
						unzLocateFile(uf, (const char*)fileUTF8, 0) == UNZ_OK &&
						unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, NULL, 0, NULL, 0) == UNZ_OK &&
						file_info.uncompressed_size > 0)
					{
						if (unzOpenCurrentFilePassword(uf, password) == UNZ_OK)
						{
							DKObject<UnZipFile> p = DKOBJECT_NEW UnZipFile(uf, file_info, password);
							return p;
						}
						else
						{
							DKLog("[%s] failed to open file: %ls.\n", DKLIB_FUNCTION_NAME, (const wchar_t*)file);
						}
					}
				}
				return NULL;
			}
Beispiel #8
0
// ////////////////////////////////////////////////////////////////////////////////
// @public @static 将指定 zip 解压至指定路径下
//
bool ZipTools::UnZip(LPCWSTR wzZipName, LPCWSTR wzDestPath)
{
	//
	// 检查指定文件和文件夹是否存在
	//
	if ( !FileTools::Exist(wzZipName) )
	{
		DebugTools::OutputDebugPrintfW(L"[ZipTools] [UnZip] File Not Exist. [%s]\r\n", wzZipName);
		return false;
	}

	if ( !FileTools::Exist(wzDestPath) )
	{
		DebugTools::OutputDebugPrintfW(L"[ZipTools] [Zip] Path Not Exist. [%s]\r\n", wzDestPath);
		return false;
	}

	// 打开 zip 文件
	zlib_filefunc64_def ffunc;
	fill_win32_filefunc64W(&ffunc);
	unzFile uf = unzOpen2_64(wzZipName, &ffunc);

	if ( NULL == uf )
	{
		DebugTools::OutputDebugPrintfW(
			L"[ZipTools] [Zip] Open Zip File Failed.[%s]\r\n", wzZipName);
	}

	// 设置指定目录为工作目录
	SetCurrentDirectoryW(wzDestPath);

	// 释放文件
	ExtractFile(uf);

	unzClose(uf);

	return true;
}
Beispiel #9
0
static void init_zlib()
{
    fill_win32_filefunc64W(&g_FFunc);
}
Beispiel #10
0
bool DialogPackage::CreatePackage()
{
	// Create options file
	WCHAR tempFile[MAX_PATH];
	GetTempPath(MAX_PATH, tempFile);
	GetTempFileName(tempFile, L"ini", 0, tempFile);

	WritePrivateProfileString(L"rmskin", L"Name", m_Name.c_str(), tempFile);
	WritePrivateProfileString(L"rmskin", L"Author", m_Author.c_str(), tempFile);
	WritePrivateProfileString(L"rmskin", L"Version", m_Version.c_str(), tempFile);

	if (!c_Dialog->m_Load.empty())
	{
		WritePrivateProfileString(L"rmskin", L"LoadType", c_Dialog->m_LoadLayout ? L"Layout" : L"Skin", tempFile);
		WritePrivateProfileString(L"rmskin", L"Load", c_Dialog->m_Load.c_str(), tempFile);
	}

	if (!c_Dialog->m_VariableFiles.empty())
	{
		WritePrivateProfileString(L"rmskin", L"VariableFiles", m_VariableFiles.c_str(), tempFile);
	}

	if (c_Dialog->m_MergeSkins)
	{
		WritePrivateProfileString(L"rmskin", L"MergeSkins", L"1", tempFile);
	}

	WritePrivateProfileString(L"rmskin", L"MinimumRainmeter", m_MinimumRainmeter.c_str(), tempFile);
	WritePrivateProfileString(L"rmskin", L"MinimumWindows", m_MinimumWindows.c_str(), tempFile);

	// Only Skin Installer in Rainmeter 3.0.1 support UTF-8 filenames.
	m_AllowNonAsciiFilenames = DialogInstall::CompareVersions(m_MinimumRainmeter, L"3.0.1") != -1;

	// Create archive and add options file and header bitmap
	zlib_filefunc64_def zlibFileFunc;
	fill_win32_filefunc64W(&zlibFileFunc);
	m_ZipFile = zipOpen2_64(m_TargetFile.c_str(), APPEND_STATUS_CREATE, nullptr, &zlibFileFunc);

	auto cleanup = [&]()->bool
	{
		zipClose(m_ZipFile, nullptr);
		DeleteFile(m_TargetFile.c_str());
		return false;
	};

	if (!m_ZipFile ||
		(!c_Dialog->m_HeaderFile.empty() && !AddFileToPackage(c_Dialog->m_HeaderFile.c_str(), L"RMSKIN.bmp")) ||
		!AddFileToPackage(tempFile, L"RMSKIN.ini"))
	{
		std::wstring error = L"Unable to create package.";
		error += L"\n\nClick OK to close Packager.";
		MessageBox(m_Window, error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
		DeleteFile(tempFile);
		return cleanup();
	}

	// Add skin
	{
		std::wstring zipPrefix = L"Skins\\" + m_SkinFolder.first;
		zipPrefix += L'\\';
		if (!AddFolderToPackage(m_SkinFolder.second, L"", zipPrefix.c_str()))
		{
			return cleanup();
		}
	}

	// Add layouts
	for (auto iter = m_LayoutFolders.cbegin(); iter != m_LayoutFolders.cend(); ++iter)
	{
		std::wstring realPath = (*iter).second + L"Rainmeter.ini";
		std::wstring zipPath = L"Layouts\\" + (*iter).first;
		zipPath += L"\\Rainmeter.ini";
		if (!AddFileToPackage(realPath.c_str(), zipPath.c_str()))
		{
			std::wstring error = L"Error adding layout '";
			error += (*iter).first;
			error += L"'.";
			error += L"\n\nClick OK to close Packager.";
			MessageBox(m_Window, error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
			return cleanup();
		}
	}

	// Add plugins
	for (auto iter = m_PluginFolders.cbegin(); iter != m_PluginFolders.cend(); ++iter)
	{
		// Add 32bit and 64bit versions
		for (int i = 0; i < 2; ++i)
		{
			const std::wstring& realPath = (i == 0) ? (*iter).second.first : (*iter).second.second;
			std::wstring zipPath = ((i == 0) ? L"Plugins\\32bit\\" : L"Plugins\\64bit\\") + (*iter).first;
			if (!AddFileToPackage(realPath.c_str(), zipPath.c_str()))
			{
				std::wstring error = L"Error adding plugin '";
				error += (*iter).first;
				error += L"'.";
				error += L"\n\nClick OK to close Packager.";
				MessageBox(m_Window, error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
				return cleanup();
			}
		}
	}

	// Add footer
	FILE* file;
	if (zipClose(m_ZipFile, nullptr) == ZIP_OK &&
		(file = _wfopen(m_TargetFile.c_str(), L"r+b")) != nullptr)
	{
		fseek(file, 0, SEEK_END);
		DialogInstall::PackageFooter footer = { _ftelli64(file), 0, "RMSKIN" };
		fwrite(&footer, sizeof(footer), 1, file);
		fclose(file);
	}
	else
	{
		std::wstring error = L"Unable to create package.";
		error += L"\n\nClick OK to close Packager.";
		MessageBox(m_Window, error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
		return false;
	}

	return true;
}
Beispiel #11
0
void ExportEPUB::SaveFolderAsEpubToLocation(const QString &fullfolderpath, const QString &fullfilepath)
{
    QString tempFile = fullfolderpath + "-tmp.epub";
    QDateTime timeNow = QDateTime::currentDateTime();
    zip_fileinfo fileInfo;
#ifdef Q_OS_WIN32
    zlib_filefunc64_def ffunc;
    fill_win32_filefunc64W(&ffunc);
    zipFile zfile = zipOpen2_64(Utility::QStringToStdWString(QDir::toNativeSeparators(tempFile)).c_str(), APPEND_STATUS_CREATE, NULL, &ffunc);
#else
    zipFile zfile = zipOpen64(QDir::toNativeSeparators(tempFile).toUtf8().constData(), APPEND_STATUS_CREATE);
#endif

    if (zfile == NULL) {
        boost_throw(CannotOpenFile() << errinfo_file_fullpath(tempFile.toStdString()));
    }

    memset(&fileInfo, 0, sizeof(fileInfo));
    fileInfo.tmz_date.tm_sec = timeNow.time().second();
    fileInfo.tmz_date.tm_min = timeNow.time().minute();
    fileInfo.tmz_date.tm_hour = timeNow.time().hour();
    fileInfo.tmz_date.tm_mday = timeNow.date().day();
    fileInfo.tmz_date.tm_mon = timeNow.date().month() - 1;
    fileInfo.tmz_date.tm_year = timeNow.date().year();

    // Write the mimetype. This must be uncompressed and the first entry in the archive.
    if (zipOpenNewFileInZip64(zfile, "mimetype", &fileInfo, NULL, 0, NULL, 0, NULL, Z_NO_COMPRESSION, 0, 0) != Z_OK) {
        zipClose(zfile, NULL);
        QFile::remove(tempFile);
        boost_throw(CannotStoreFile() << errinfo_file_fullpath("mimetype"));
    }

    if (zipWriteInFileInZip(zfile, EPUB_MIME_DATA, (unsigned int)strlen(EPUB_MIME_DATA)) != Z_OK) {
        zipCloseFileInZip(zfile);
        zipClose(zfile, NULL);
        QFile::remove(tempFile);
        boost_throw(CannotStoreFile() << errinfo_file_fullpath("mimetype"));
    }

    zipCloseFileInZip(zfile);
    // Write all the files in our directory path to the archive.
    QDirIterator it(fullfolderpath, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden, QDirIterator::Subdirectories);

    while (it.hasNext()) {
        it.next();
        QString relpath = it.filePath().remove(fullfolderpath);

        while (relpath.startsWith("/")) {
            relpath = relpath.remove(0, 1);
        }

        // Add the file entry to the archive.
        // We should check the uncompressed file size. If it's over >= 0xffffffff the last parameter (zip64) should be 1.
        if (zipOpenNewFileInZip4_64(zfile, relpath.toUtf8().constData(), &fileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 8, 0, 15, 8, Z_DEFAULT_STRATEGY, NULL, 0, 0x0b00, 1<<11, 0) != Z_OK) {
            zipClose(zfile, NULL);
            QFile::remove(tempFile);
            boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString()));
        }

        // Open the file on disk. We will read this and write what we read into
        // the archive.
        QFile dfile(it.filePath());

        if (!dfile.open(QIODevice::ReadOnly)) {
            zipCloseFileInZip(zfile);
            zipClose(zfile, NULL);
            QFile::remove(tempFile);
            boost_throw(CannotOpenFile() << errinfo_file_fullpath(it.fileName().toStdString()));
        }

        // Write the data from the file on disk into the archive.
        char buff[BUFF_SIZE] = {0};
        qint64 read = 0;

        while ((read = dfile.read(buff, BUFF_SIZE)) > 0) {
            if (zipWriteInFileInZip(zfile, buff, read) != Z_OK) {
                dfile.close();
                zipCloseFileInZip(zfile);
                zipClose(zfile, NULL);
                QFile::remove(tempFile);
                boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString()));
            }
        }

        dfile.close();

        // There was an error reading the file on disk.
        if (read < 0) {
            zipCloseFileInZip(zfile);
            zipClose(zfile, NULL);
            QFile::remove(tempFile);
            boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString()));
        }

        if (zipCloseFileInZip(zfile) != Z_OK) {
            zipClose(zfile, NULL);
            QFile::remove(tempFile);
            boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString()));
        }
    }

    zipClose(zfile, NULL);
    // Overwrite the contents of the real file with the contents from the temp
    // file we saved the data do. We do this instead of simply copying the file
    // because a file copy will lose extended attributes such as labels on OS X.
    QFile temp_epub(tempFile);

    if (!temp_epub.open(QFile::ReadOnly)) {
        boost_throw(CannotOpenFile() << errinfo_file_fullpath(tempFile.toStdString()));
    }

    QFile real_epub(fullfilepath);

    if (!real_epub.open(QFile::WriteOnly | QFile::Truncate)) {
        temp_epub.close();
        boost_throw(CannotWriteFile() << errinfo_file_fullpath(fullfilepath.toStdString()));
    }

    // Copy the contents from the temp file to the real file.
    char buff[BUFF_SIZE] = {0};
    qint64 read = 0;
    qint64 written = 0;

    while ((read = temp_epub.read(buff, BUFF_SIZE)) > 0) {
        written = real_epub.write(buff, read);

        if (written != read) {
            temp_epub.close();
            real_epub.close();
            QFile::remove(tempFile);
            boost_throw(CannotCopyFile() << errinfo_file_fullpath(fullfilepath.toStdString()));
        }
    }

    if (read == -1) {
        temp_epub.close();
        real_epub.close();
        QFile::remove(tempFile);
        boost_throw(CannotCopyFile() << errinfo_file_fullpath(fullfilepath.toStdString()));
    }

    temp_epub.close();
    real_epub.close();
    QFile::remove(tempFile);
}