示例#1
0
// ////////////////////////////////////////////////////////////////////////////////
// @global 将文件夹内文件添加进 zip ( 递归 )
//
void AddFileToZip(zipFile& zf, LPCWSTR wzPath, LPCWSTR wzParentPath, PBYTE buf)
{
	WIN32_FIND_DATAW ffd = {0};
	WCHAR wzFindStr[MAX_PATH] = {0};
	
	wsprintf(wzFindStr, L"%s\\*", wzPath);
	HANDLE hFind = FindFirstFileW(wzFindStr, &ffd);

	do 
	{
		if ( 0 == wcscmp(ffd.cFileName, L".")
			|| 0 == wcscmp(ffd.cFileName, L"..") )
		{
			continue;
		}

		if ( ffd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
		{
			WCHAR wzSubDir[MAX_PATH] = {0};
			wsprintf(wzSubDir, L"%s\\%s", wzPath, ffd.cFileName);
			AddFileToZip(zf, wzSubDir, wzParentPath, buf);
			continue;
		}

		WCHAR wzFullPath[MAX_PATH] = {0};
		wsprintf(wzFullPath, L"%s\\%s", wzPath, ffd.cFileName);
		WriteFileToZip(ffd, zf, wzFullPath, wzParentPath, buf);

		DebugTools::OutputDebugPrintfW(L"[ZipTools] [AddFileToZip] Add File Success. [%s]\r\n", wzFullPath);

	} while ( 0 != FindNextFileW(hFind, &ffd));
}
示例#2
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;
}
示例#3
0
bool ZipWrapper::AddFolderToZip(const std::tstring& sFilename)
{
	WIN32_FIND_DATA fd;

	HANDLE hFile = FindFirstFile(CStdString(sFilename + _T("\\*.*")).c_str(), &fd);
	
	if (hFile == INVALID_HANDLE_VALUE)
		return false;

	while (FindNextFile(hFile, &fd))
	{
		std::tstring sFile = fd.cFileName;
		if (sFile.compare(_T("..")) == 0)
			continue;
		if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			AddFolderToZip(sFilename + _T("\\") + fd.cFileName);
		else AddFileToZip(sFilename + _T("\\") + fd.cFileName);
	}
	FindClose(hFile);
	return true;
}
示例#4
0
bool ZipWrapper::AddFileToZipA(const std::string& sFilename)
{
	return AddFileToZip(CStdString(sFilename));
}
示例#5
0
bool CZipper::AddFolderToZip(LPCTSTR szFolderPath, bool bIgnoreFilePath)
{
	if (!m_uzFile)
		return FALSE;

	m_info.nFolderCount++;

	// if the path is relative then we need to append the root before opening
	char szFullPath[MAX_PATH];
	
	lstrcpy(szFullPath, szFolderPath);
	PrepareSourcePath(szFullPath);

	// always add folder first
	// save file attributes
	zip_fileinfo zfi;
	
	zfi.internal_fa = 0;
	zfi.external_fa = ::GetFileAttributes(szFullPath);
	
	SYSTEMTIME st;
	
//	GetLastModified(szFullPath, st, TRUE);
	
	zfi.dosDate = 0;
	zfi.tmz_date.tm_year = st.wYear;
	zfi.tmz_date.tm_mon = st.wMonth - 1;
	zfi.tmz_date.tm_mday = st.wDay;
	zfi.tmz_date.tm_hour = st.wHour;
	zfi.tmz_date.tm_min = st.wMinute;
	zfi.tmz_date.tm_sec = st.wSecond;
	
	// if the folder is a fullpath then remove the root path bit
	char szFolderName[MAX_PATH] = "";
	
	if (bIgnoreFilePath)
	{
		_splitpath(szFullPath, NULL, NULL, szFolderName, NULL);
	}
	else
	{
		// check the root can be found
		if (0 != _strnicmp(szFullPath, m_szRootFolder, lstrlen(m_szRootFolder)))
			return false;
		
		// else
		lstrcpy(szFolderName, szFullPath + lstrlen(m_szRootFolder));
	}
	
	// folders are denoted by a trailing '\\'
	lstrcat(szFolderName, "\\");
	
	// open the file in the zip making sure we remove any leading '\'
	int nRet = zipOpenNewFileInZip(m_uzFile, 
		szFolderName,
		&zfi, 
		NULL, 
		0,
		NULL,
		0, 
		NULL,
		Z_DEFLATED,
		Z_DEFAULT_COMPRESSION);
	
	zipCloseFileInZip(m_uzFile);

	// build searchspec
	char szDrive[_MAX_DRIVE], szFolder[MAX_PATH], szName[_MAX_FNAME];
	_splitpath(szFullPath, szDrive, szFolder, szName, NULL);
	lstrcat(szFolder, szName);

	char szSearchSpec[MAX_PATH];
	_makepath(szSearchSpec, szDrive, szFolder, "*", "*");

	WIN32_FIND_DATA finfo;
	HANDLE hSearch = FindFirstFile(szSearchSpec, &finfo);

	if (hSearch != INVALID_HANDLE_VALUE) 
	{
		do 
		{
			if (finfo.cFileName[0] != '.') 
			{
				char szItem[MAX_PATH];
				_makepath(szItem, szDrive, szFolder, finfo.cFileName, NULL);
				
				if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					AddFolderToZip(szItem, bIgnoreFilePath);
				}
				else 
					AddFileToZip(szItem, bIgnoreFilePath);
			}
		} 
		while (FindNextFile(hSearch, &finfo));
		
		FindClose(hSearch);
	}

	return TRUE;
}