CZipString CZipPathComponent::GetNoDrive() const { CZipString szPath = m_szDirectory; CZipString szFileName = GetFileName(); if (!szFileName.IsEmpty() && !szPath.IsEmpty()) szPath += m_cSeparator; szPath += szFileName; return szPath; }
BOOL CZipException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError, UINT* ) { if (!lpszError || !nMaxError) return FALSE; CZipString sz = GetErrorDescription(); if (sz.IsEmpty()) return FALSE; UINT iLen = sz.GetLength(); if (nMaxError - 1 < iLen) iLen = nMaxError - 1; LPTSTR lpsz = sz.GetBuffer(iLen); #if _MSC_VER >= 1400 #ifdef _UNICODE wcsncpy_s(lpszError, nMaxError, lpsz, iLen); #else strncpy_s(lpszError, nMaxError, lpsz, iLen); #endif #else #ifdef _UNICODE wcsncpy(lpszError, lpsz, iLen); #else strncpy(lpszError, lpsz, iLen); #endif #endif lpszError[iLen] = _T('\0'); return TRUE; }
void FillFromFile(FILELIST& l, LPCTSTR lpszFile, bool bCheck) { FILE* f; #if _MSC_VER >= 1400 if (fopen_s(&f, lpszFile, "rt") != 0) f = NULL; #else f = fopen(lpszFile, "rt"); #endif if (!f) { printf ("File %s could not be opened\n", lpszFile); return; } fseek(f, 0, SEEK_END); int iSize = ftell(f); fseek(f, 0, SEEK_SET); CZipAutoBuffer buf(iSize + 1); iSize = fread(buf, 1, iSize, f); fclose(f); char* sEnd = buf + iSize; char* sBeg = buf; for (char* pos = buf; ; pos++) { bool bEnd = pos == sEnd; // there may be no newline at the end if (strncmp(pos, "\n", 1) == 0 || bEnd) { *pos = '\0'; CZipString s = sBeg; s.TrimLeft(" "); s.TrimRight(" "); if (!s.IsEmpty() && (!bCheck || ZipPlatform::FileExists(s) != 0)) l.push_back(s); if (bEnd) break; sBeg = pos + 1; } } }
CZipString ZipPlatform::GetTmpFileName(LPCTSTR lpszPath, ZIP_U32_U64 uSizeNeeded) { TCHAR empty[] = _T(""), prefix [] = _T("zar"); TCHAR* buf = NULL; CZipString tempPath = lpszPath; if (tempPath.IsEmpty()) tempPath = "/tmp"; if (ZipPlatform::GetDeviceFreeSpace(tempPath) < uSizeNeeded) return empty; CZipPathComponent::AppendSeparator(tempPath); tempPath += prefix; tempPath += _T("XXXXXX"); int handle = mkstemp(tempPath.GetBuffer(tempPath.GetLength())); tempPath.ReleaseBuffer(); if (handle != -1) { close(handle); // we just create the file and open it later return tempPath; } else return empty; }
bool CZipFile::Open(LPCTSTR lpszFileName, UINT openFlags, bool bThrow) { if (!IsClosed()) Close(); CZipString fileName = lpszFileName; if (fileName.IsEmpty()) { return false; } DWORD access; DWORD temp = openFlags & 3; if (temp == modeWrite) { access = GENERIC_WRITE; } else if (temp == modeReadWrite) { access = GENERIC_READ | GENERIC_WRITE; } else { access = GENERIC_READ; } DWORD share; temp = openFlags & 0x70; if (temp == shareDenyWrite) { share = FILE_SHARE_READ; } else if (temp == shareDenyRead) { share = FILE_SHARE_WRITE; } else if (temp == shareDenyNone) { share = FILE_SHARE_READ | FILE_SHARE_WRITE; } else { share = 0; } CZipPathComponent::AddPrefix(fileName, false); DWORD create; if (openFlags & modeCreate) { if (openFlags & modeNoTruncate) { create = OPEN_ALWAYS; } else { create = CREATE_ALWAYS; } } else { create = OPEN_EXISTING; } SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; m_hFile = ::CreateFile(fileName, access, share, &sa, create, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { if (bThrow) ThrowError(); else return false; } m_szFileName = lpszFileName; return true; }
bool CSimpleZip::Add(std::string strZipFile,std::list<std::string>& lstFile,std::string strRootPath,bool bFullPath) { CZipArchive zip; CZipString szArchive; int iVolumeSize = 0; int iMode = CZipArchive::zipOpen; szArchive =CZipString(strZipFile); if (!ZipPlatform::FileExists(szArchive)) iMode = CZipArchive::zipCreate; CZipPathComponent zpc(szArchive); SpanCallback span; zip.SetSpanCallback(&span); try { zip.Open(szArchive, iMode, iVolumeSize); } catch(...) { return FALSE; } zip.SetRootPath(strRootPath.c_str()); FILELIST lFiles; for(std::list<std::string>::iterator it =lstFile.begin();it!=lstFile.end();it++) { std::string strFileName; strFileName =*it; lFiles.push_back(CZipString(strFileName)); } FILELISTADD rev; for (FILELISTIT it = lFiles.begin(); it != lFiles.end(); ++it) { CZipString sz = zip.PredictFileNameInZip(*it, bFullPath); if (!sz.IsEmpty()) rev.push_back(CZipAddFileInfo(*it, sz)); } lFiles.clear(); // rev.sort(std::greater<CZipAddFileInfo>()); FILELISTADDIT it1; int iSmartLevel = CZipArchive::zipsmSafeSmart; for (it1 = rev.begin(); it1 != rev.end(); ++it1) { if (zip.AddNewFile((*it1).m_szFilePath, 5, bFullPath, iSmartLevel)) { printf ("%s added\n", (LPCTSTR)(*it1).m_szFileNameInZip); } else printf ("%s not added\n", (LPCTSTR)(*it1).m_szFilePath); } zip.Close(); return TRUE; }
void AddDirectory(CZipString szPath, struct AddDirectoryInfo& info, bool bDoNotAdd) { if (!szPath.IsEmpty()) CZipPathComponent::AppendSeparator(szPath); bool bPathAdded = info.m_bAddEmpty || bDoNotAdd; if (info.m_bAddEmpty && !szPath.IsEmpty() && !bDoNotAdd) info.m_l.push_back(szPath); #ifdef __GNUC__ DIR* dp = opendir(szPath.IsEmpty() ? "." : szPath); if (!dp) return; struct dirent* entry; while (entry = readdir(dp)) { struct stat sStats; CZipString szFullFileName = szPath + entry->d_name; if (stat(szFullFileName, &sStats) == -1) continue; if (S_ISDIR(sStats.st_mode)) { if (info.m_bRecursive) { if (IsDots(entry->d_name)) continue; AddDirectory(szFullFileName, info, false); } } else if (fnmatch(info.m_lpszFile, entry->d_name, FNM_NOESCAPE |FNM_PATHNAME) == 0) { if (!bPathAdded) { if (!szPath.IsEmpty()) info.m_l.push_back(szPath); bPathAdded = true; } info.m_l.push_back(szPath + entry->d_name); } } closedir(dp); #else CZipString szFullFileName = szPath + info.m_lpszFile; struct _finddata_t c_file; long hFile; if( (hFile = _findfirst( szFullFileName, &c_file )) != -1L ) { do { if (!(c_file.attrib & FILE_ATTRIBUTE_DIRECTORY)) { // add it when the first file comes if (!bPathAdded) { if (!szPath.IsEmpty()) info.m_l.push_back(szPath); bPathAdded = true; } info.m_l.push_back(szPath + c_file.name); } } while (_findnext(hFile, &c_file) == 0L); } _findclose(hFile); if (info.m_bRecursive) { szFullFileName = szPath + "*"; if( (hFile = _findfirst( szFullFileName, &c_file )) != -1L ) { do { if (c_file.attrib & FILE_ATTRIBUTE_DIRECTORY) { if (IsDots(c_file.name)) continue; szFullFileName = szPath + c_file.name; AddDirectory(szFullFileName, info, false); } } while (_findnext(hFile, &c_file) == 0L); } _findclose(hFile); } #endif }