2019-12-01 12:20:53 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Zip Shell Extension
|
|
|
|
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
|
|
|
|
* PURPOSE: Create a zip file
|
|
|
|
* COPYRIGHT: Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
|
2023-07-22 03:57:32 +00:00
|
|
|
* Copyright 2019-2023 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
|
2019-12-01 12:20:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
#include "atlsimpcoll.h"
|
|
|
|
#include "minizip/zip.h"
|
|
|
|
#include "minizip/iowin32.h"
|
|
|
|
#include <process.h>
|
|
|
|
|
2023-07-17 11:12:45 +00:00
|
|
|
static CStringW DoGetZipName(PCWSTR filename)
|
2019-12-01 12:20:53 +00:00
|
|
|
{
|
|
|
|
WCHAR szPath[MAX_PATH];
|
|
|
|
StringCbCopyW(szPath, sizeof(szPath), filename);
|
|
|
|
PathRemoveExtensionW(szPath);
|
|
|
|
|
|
|
|
CStringW ret = szPath;
|
|
|
|
ret += L".zip";
|
|
|
|
|
|
|
|
UINT i = 2;
|
|
|
|
while (PathFileExistsW(ret))
|
|
|
|
{
|
|
|
|
CStringW str;
|
|
|
|
str.Format(L" (%u).zip", i++);
|
|
|
|
|
|
|
|
ret = szPath;
|
|
|
|
ret += str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-07-17 11:12:45 +00:00
|
|
|
static CStringW DoGetBaseName(PCWSTR filename)
|
2019-12-01 12:20:53 +00:00
|
|
|
{
|
|
|
|
WCHAR szBaseName[MAX_PATH];
|
|
|
|
StringCbCopyW(szBaseName, sizeof(szBaseName), filename);
|
|
|
|
PathRemoveFileSpecW(szBaseName);
|
|
|
|
PathAddBackslashW(szBaseName);
|
|
|
|
return szBaseName;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CStringA
|
2023-07-22 03:57:32 +00:00
|
|
|
DoGetNameInZip(const CStringW& basename, const CStringW& filename, UINT nCodePage)
|
2019-12-01 12:20:53 +00:00
|
|
|
{
|
|
|
|
CStringW basenameI = basename, filenameI = filename;
|
|
|
|
basenameI.MakeUpper();
|
|
|
|
filenameI.MakeUpper();
|
|
|
|
|
|
|
|
CStringW ret;
|
|
|
|
if (filenameI.Find(basenameI) == 0)
|
|
|
|
ret = filename.Mid(basename.GetLength());
|
|
|
|
else
|
|
|
|
ret = filename;
|
|
|
|
|
|
|
|
ret.Replace(L'\\', L'/');
|
|
|
|
|
2023-07-22 03:57:32 +00:00
|
|
|
return CStringA(CW2AEX<MAX_PATH>(ret, nCodePage));
|
2019-12-01 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
2023-07-17 11:12:45 +00:00
|
|
|
DoReadAllOfFile(PCWSTR filename, CSimpleArray<BYTE>& contents,
|
2019-12-01 12:20:53 +00:00
|
|
|
zip_fileinfo *pzi)
|
|
|
|
{
|
|
|
|
contents.RemoveAll();
|
|
|
|
|
|
|
|
HANDLE hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING,
|
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DPRINT1("%S: cannot open\n", filename);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILETIME ft, ftLocal;
|
|
|
|
ZeroMemory(pzi, sizeof(*pzi));
|
|
|
|
if (GetFileTime(hFile, NULL, NULL, &ft))
|
|
|
|
{
|
|
|
|
SYSTEMTIME st;
|
|
|
|
FileTimeToLocalFileTime(&ft, &ftLocal);
|
|
|
|
FileTimeToSystemTime(&ftLocal, &st);
|
|
|
|
pzi->tmz_date.tm_sec = st.wSecond;
|
|
|
|
pzi->tmz_date.tm_min = st.wMinute;
|
|
|
|
pzi->tmz_date.tm_hour = st.wHour;
|
|
|
|
pzi->tmz_date.tm_mday = st.wDay;
|
|
|
|
pzi->tmz_date.tm_mon = st.wMonth - 1;
|
|
|
|
pzi->tmz_date.tm_year = st.wYear;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DWORD cbBuff = 0x7FFF;
|
|
|
|
LPBYTE pbBuff = reinterpret_cast<LPBYTE>(CoTaskMemAlloc(cbBuff));
|
|
|
|
if (!pbBuff)
|
|
|
|
{
|
|
|
|
DPRINT1("Out of memory\n");
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
DWORD cbRead;
|
|
|
|
if (!ReadFile(hFile, pbBuff, cbBuff, &cbRead, NULL) || !cbRead)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < cbRead; ++i)
|
|
|
|
contents.Add(pbBuff[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoTaskMemFree(pbBuff);
|
|
|
|
CloseHandle(hFile);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-07-17 11:12:45 +00:00
|
|
|
DoAddFilesFromItem(CSimpleArray<CStringW>& files, PCWSTR item)
|
2019-12-01 12:20:53 +00:00
|
|
|
{
|
|
|
|
if (!PathIsDirectoryW(item))
|
|
|
|
{
|
|
|
|
files.Add(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR szPath[MAX_PATH];
|
|
|
|
StringCbCopyW(szPath, sizeof(szPath), item);
|
|
|
|
PathAppendW(szPath, L"*");
|
|
|
|
|
|
|
|
WIN32_FIND_DATAW find;
|
|
|
|
HANDLE hFind = FindFirstFileW(szPath, &find);
|
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (wcscmp(find.cFileName, L".") == 0 ||
|
|
|
|
wcscmp(find.cFileName, L"..") == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringCbCopyW(szPath, sizeof(szPath), item);
|
|
|
|
PathAppendW(szPath, find.cFileName);
|
|
|
|
|
|
|
|
if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
DoAddFilesFromItem(files, szPath);
|
|
|
|
else
|
|
|
|
files.Add(szPath);
|
|
|
|
} while (FindNextFileW(hFind, &find));
|
|
|
|
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CZipCreatorImpl
|
|
|
|
{
|
|
|
|
CSimpleArray<CStringW> m_items;
|
|
|
|
|
|
|
|
unsigned JustDoIt();
|
|
|
|
};
|
|
|
|
|
|
|
|
CZipCreator::CZipCreator() : m_pimpl(new CZipCreatorImpl)
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&g_ModuleRefCnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
CZipCreator::~CZipCreator()
|
|
|
|
{
|
|
|
|
InterlockedDecrement(&g_ModuleRefCnt);
|
|
|
|
delete m_pimpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned __stdcall
|
|
|
|
create_zip_function(void *arg)
|
|
|
|
{
|
2019-12-01 17:26:34 +00:00
|
|
|
CZipCreator *pCreator = reinterpret_cast<CZipCreator *>(arg);
|
|
|
|
return pCreator->m_pimpl->JustDoIt();
|
2019-12-01 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:26:34 +00:00
|
|
|
BOOL CZipCreator::runThread(CZipCreator *pCreator)
|
2019-12-01 12:20:53 +00:00
|
|
|
{
|
|
|
|
unsigned tid = 0;
|
|
|
|
HANDLE hThread = reinterpret_cast<HANDLE>(
|
2019-12-01 17:26:34 +00:00
|
|
|
_beginthreadex(NULL, 0, create_zip_function, pCreator, 0, &tid));
|
2019-12-01 12:20:53 +00:00
|
|
|
|
|
|
|
if (hThread)
|
|
|
|
{
|
|
|
|
CloseHandle(hThread);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINT1("hThread == NULL\n");
|
|
|
|
|
|
|
|
CStringW strTitle(MAKEINTRESOURCEW(IDS_ERRORTITLE));
|
|
|
|
CStringW strText(MAKEINTRESOURCEW(IDS_CANTSTARTTHREAD));
|
|
|
|
MessageBoxW(NULL, strText, strTitle, MB_ICONERROR);
|
|
|
|
|
2019-12-01 17:26:34 +00:00
|
|
|
delete pCreator;
|
2019-12-01 12:20:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-07-17 11:12:45 +00:00
|
|
|
void CZipCreator::DoAddItem(PCWSTR pszFile)
|
2019-12-01 12:20:53 +00:00
|
|
|
{
|
|
|
|
// canonicalize path
|
|
|
|
WCHAR szPath[MAX_PATH];
|
|
|
|
GetFullPathNameW(pszFile, _countof(szPath), szPath, NULL);
|
|
|
|
|
|
|
|
m_pimpl->m_items.Add(szPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum CZC_ERROR
|
|
|
|
{
|
|
|
|
CZCERR_ZEROITEMS = 1,
|
|
|
|
CZCERR_NOFILES,
|
|
|
|
CZCERR_CREATE,
|
|
|
|
CZCERR_READ
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned CZipCreatorImpl::JustDoIt()
|
|
|
|
{
|
|
|
|
// TODO: Show progress.
|
|
|
|
|
|
|
|
if (m_items.GetSize() <= 0)
|
|
|
|
{
|
|
|
|
DPRINT1("GetSize() <= 0\n");
|
|
|
|
return CZCERR_ZEROITEMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimpleArray<CStringW> files;
|
|
|
|
for (INT iItem = 0; iItem < m_items.GetSize(); ++iItem)
|
|
|
|
{
|
|
|
|
DoAddFilesFromItem(files, m_items[iItem]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.GetSize() <= 0)
|
|
|
|
{
|
|
|
|
DPRINT1("files.GetSize() <= 0\n");
|
|
|
|
|
|
|
|
CStringW strTitle(MAKEINTRESOURCEW(IDS_ERRORTITLE));
|
|
|
|
CStringW strText;
|
2023-07-17 11:12:45 +00:00
|
|
|
strText.Format(IDS_NOFILES, static_cast<PCWSTR>(m_items[0]));
|
2019-12-01 12:20:53 +00:00
|
|
|
MessageBoxW(NULL, strText, strTitle, MB_ICONERROR);
|
|
|
|
|
|
|
|
return CZCERR_NOFILES;
|
|
|
|
}
|
|
|
|
|
|
|
|
zlib_filefunc64_def ffunc;
|
|
|
|
fill_win32_filefunc64W(&ffunc);
|
|
|
|
|
|
|
|
CStringW strZipName = DoGetZipName(m_items[0]);
|
|
|
|
zipFile zf = zipOpen2_64(strZipName, APPEND_STATUS_CREATE, NULL, &ffunc);
|
|
|
|
if (zf == 0)
|
|
|
|
{
|
|
|
|
DPRINT1("zf == 0\n");
|
|
|
|
|
|
|
|
int err = CZCERR_CREATE;
|
|
|
|
|
|
|
|
CStringW strTitle(MAKEINTRESOURCEW(IDS_ERRORTITLE));
|
|
|
|
CStringW strText;
|
2023-07-17 11:12:45 +00:00
|
|
|
strText.Format(IDS_CANTCREATEZIP, static_cast<PCWSTR>(strZipName), err);
|
2019-12-01 12:20:53 +00:00
|
|
|
MessageBoxW(NULL, strText, strTitle, MB_ICONERROR);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: password
|
|
|
|
const char *password = NULL;
|
|
|
|
int zip64 = 1; // always zip64
|
|
|
|
zip_fileinfo zi;
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
CStringW strTarget, strBaseName = DoGetBaseName(m_items[0]);
|
2023-07-22 03:57:32 +00:00
|
|
|
UINT nCodePage = GetZipCodePage(FALSE);
|
2019-12-01 12:20:53 +00:00
|
|
|
for (INT iFile = 0; iFile < files.GetSize(); ++iFile)
|
|
|
|
{
|
|
|
|
const CStringW& strFile = files[iFile];
|
|
|
|
|
|
|
|
CSimpleArray<BYTE> contents;
|
|
|
|
if (!DoReadAllOfFile(strFile, contents, &zi))
|
|
|
|
{
|
|
|
|
DPRINT1("DoReadAllOfFile failed\n");
|
|
|
|
err = CZCERR_READ;
|
|
|
|
strTarget = strFile;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long crc = 0;
|
|
|
|
if (password)
|
|
|
|
{
|
|
|
|
// TODO: crc = ...;
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:57:32 +00:00
|
|
|
CStringA strNameInZip = DoGetNameInZip(strBaseName, strFile, nCodePage);
|
|
|
|
err = zipOpenNewFileInZip4_64(zf,
|
2019-12-01 12:20:53 +00:00
|
|
|
strNameInZip,
|
|
|
|
&zi,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
Z_DEFLATED,
|
|
|
|
Z_DEFAULT_COMPRESSION,
|
|
|
|
0,
|
|
|
|
-MAX_WBITS,
|
|
|
|
DEF_MEM_LEVEL,
|
|
|
|
Z_DEFAULT_STRATEGY,
|
|
|
|
password,
|
|
|
|
crc,
|
2023-07-22 03:57:32 +00:00
|
|
|
MINIZIP_COMPATIBLE_VERSION,
|
|
|
|
(nCodePage == CP_UTF8 ? MINIZIP_UTF8_FLAG : 0),
|
2019-12-01 12:20:53 +00:00
|
|
|
zip64);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
DPRINT1("zipOpenNewFileInZip3_64\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = zipWriteInFileInZip(zf, contents.GetData(), contents.GetSize());
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
DPRINT1("zipWriteInFileInZip\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = zipCloseFileInZip(zf);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
DPRINT1("zipCloseFileInZip\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zipClose(zf, NULL);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
DeleteFileW(strZipName);
|
|
|
|
|
|
|
|
CStringW strTitle(MAKEINTRESOURCEW(IDS_ERRORTITLE));
|
|
|
|
|
|
|
|
CStringW strText;
|
|
|
|
if (err < 0)
|
2023-07-17 11:12:45 +00:00
|
|
|
strText.Format(IDS_CANTCREATEZIP, static_cast<PCWSTR>(strZipName), err);
|
2019-12-01 12:20:53 +00:00
|
|
|
else
|
2023-07-17 11:12:45 +00:00
|
|
|
strText.Format(IDS_CANTREADFILE, static_cast<PCWSTR>(strTarget));
|
2019-12-01 12:20:53 +00:00
|
|
|
|
|
|
|
MessageBoxW(NULL, strText, strTitle, MB_ICONERROR);
|
|
|
|
}
|
2020-04-10 09:16:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
WCHAR szFullPath[MAX_PATH];
|
|
|
|
GetFullPathNameW(strZipName, _countof(szFullPath), szFullPath, NULL);
|
|
|
|
SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, szFullPath, NULL);
|
|
|
|
}
|
2019-12-01 12:20:53 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|