1 ///////////////////////////////////////////////////////////////////////////////
7 // Copyright (c) 2003 Michael Carruth
9 ///////////////////////////////////////////////////////////////////////////////
16 //////////////////////////////////////////////////////////////////////
17 // Construction/Destruction
18 //////////////////////////////////////////////////////////////////////
21 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
35 // Close open zip file
44 //-----------------------------------------------------------------------------
47 // Create or open zip file
49 BOOL
CZLib::Open(string f_file
, int f_nAppend
)
51 m_zf
= zipOpen(f_file
.c_str(), f_nAppend
);
52 return (m_zf
!= NULL
);
56 //-----------------------------------------------------------------------------
59 // Close open zip file
70 //-----------------------------------------------------------------------------
73 // Adds a file to the zip archive
75 BOOL
CZLib::AddFile(string f_file
)
79 // Open file being added
81 hFile
= CreateFile(f_file
.c_str(), GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
84 // Get file creation date
85 FILETIME ft
= CUtility::getLastWriteFileTime(f_file
);
86 zip_fileinfo zi
= {0};
88 FileTimeToDosDateTime(
89 &ft
, // last write FILETIME
90 ((LPWORD
)&zi
.dosDate
)+1, // dos date
91 ((LPWORD
)&zi
.dosDate
)+0); // dos time
93 // Trim path off file name
94 string sFileName
= f_file
.substr(f_file
.find_last_of(_T('\\')) + 1);
96 // Start a new file in Zip
97 if (ZIP_OK
== zipOpenNewFileInZip(m_zf
,
108 // Write file to Zip in 4 KB chunks
109 const DWORD BUFFSIZE
= 4096;
110 TCHAR buffer
[BUFFSIZE
] = _T("");
111 DWORD dwBytesRead
= 0;
113 while (ReadFile(hFile
, &buffer
, BUFFSIZE
, &dwBytesRead
, NULL
)
116 if (ZIP_OK
== zipWriteInFileInZip(m_zf
, buffer
, dwBytesRead
)
117 && dwBytesRead
< BUFFSIZE
)
124 bReturn
&= (ZIP_OK
== zipCloseFileInZip(m_zf
));
127 bReturn
&= CloseHandle(hFile
);