unzip fix + add tests

This commit is contained in:
Alexey
2022-12-11 18:00:14 +03:00
parent 2212c3b26f
commit 2942452a61
3 changed files with 129 additions and 25 deletions

View File

@ -219,39 +219,36 @@ namespace ZLibZipUtils
}
static int do_extract_currentfile( unzFile uf, const WCHAR* unzip_dir, const int* popt_extract_without_path, int* popt_overwrite, const char* password )
{
char filename_inzipA[256];
wchar_t filename_inzip[256];
wchar_t* filename_withoutpath = NULL;
wchar_t* p = NULL;
char filename_inzipA[4096];
int err = UNZ_OK;
unz_file_info file_info;
err = unzGetCurrentFileInfo(uf,&file_info,filename_inzipA,sizeof(filename_inzipA),NULL,0,NULL,0);
wchar_t wsep = FILE_SEPARATOR_CHAR == '\\' ? L'\\' : L'/';
std::wstring filenameW = codepage_issue_fixFromOEM(filename_inzipA);
wcscpy(filename_inzip , filenameW.c_str());
std::wstring filenameW_withoutpath;
std::wstring output = std::wstring(unzip_dir) + L"/" + std::wstring(filename_inzip);
for(int i = 0; i < output.size(); i++)
if(output[i] == L'\\')
output[i] = L'/';;
for(int i = 0; i < filenameW.size(); i++)
if(filenameW[i] == L'/' && wsep != filenameW[i])
filenameW[i] = wsep;
std::wstring output = std::wstring(unzip_dir) + wsep + filenameW;
if (err!=UNZ_OK)
{
return err;
}
p = filename_withoutpath = filename_inzip;
while ((*p) != '\0')
{
if (((*p)=='/') || ((*p)=='\\'))
filename_withoutpath = p+1;
p++;
}
size_t pos = 0;
for(int i = 0; i < filenameW.size(); i++)
if (filenameW[i] == wsep)
pos = i + 1;
if ((*filename_withoutpath)=='\0')
if(pos < filenameW.size())
filenameW_withoutpath = filenameW.substr(pos, filenameW.size() - pos);
if (filenameW_withoutpath.empty())
{
if ((*popt_extract_without_path)==0)
{
@ -266,7 +263,7 @@ namespace ZLibZipUtils
if ((*popt_extract_without_path)==0)
write_filename = output.c_str();
else
write_filename = filename_withoutpath;
write_filename = (std::wstring(unzip_dir) + wsep + filenameW_withoutpath).c_str();
err = unzOpenCurrentFilePassword(uf, password);
if (((*popt_overwrite)==0) && (err==UNZ_OK))
@ -322,13 +319,9 @@ namespace ZLibZipUtils
// some zipfile don't contain directory alone before file
if ((fout == NULL) && ((*popt_extract_without_path)==0) &&
(filename_withoutpath!=(wchar_t*)filename_inzip))
(filenameW_withoutpath!=filenameW))
{
char c=*(filename_withoutpath-1);
*(filename_withoutpath-1)='\0';
makedir(write_filename);
*(filename_withoutpath-1)=c;
if(oFile.CreateFileW(write_filename))
{

View File

@ -0,0 +1,82 @@
#include <iostream>
#include "../src/OfficeUtils.h"
#include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/common/Directory.h"
#include "../../DesktopEditor/xml/include/xmlutils.h"
#include "../../DesktopEditor/common/StringBuilder.h"
int main(int argc, char *argv[])
{
std::wstring sep;
#ifdef WIN32
sep = L"\\";
#else
sep = L"/";
#endif // WIN32
COfficeUtils utils;
std::wstring curr_dir = NSFile::GetProcessDirectory();
std::wstring output_dir = curr_dir + sep + L"output";
std::wstring input_dir = curr_dir + sep + L"input";
std::wstring temp_dir = curr_dir + sep + L"temp";
if(NSDirectory::Exists(input_dir))
NSDirectory::DeleteDirectory(input_dir);
NSDirectory::CreateDirectory(input_dir);
if(NSDirectory::Exists(output_dir))
NSDirectory::DeleteDirectory(output_dir);
NSDirectory::CreateDirectory(output_dir);
if(NSDirectory::Exists(temp_dir))
NSDirectory::DeleteDirectory(temp_dir);
NSDirectory::CreateDirectory(temp_dir);
std::vector<std::wstring> tests;
tests.push_back(L"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890");
tests.push_back(L"-_=+[{]};\',.`~!@#$%^&()!№;%()");
tests.push_back(L"こんにちは世界 مرحبا بالعالم");
tests.push_back(L"⌦ⵄ⬺▨⿐⸌⍒⿆₟⹖Ⅼ⌡┓ⓡ⣾⩬⍠⊆≆⳩≾⃉⌮⢸⠐⩏„⨑☰⟬∬⤢⹛⮚∅✢⓴✎ℙ⥅ⅱ‟⥩⩲⺐⫒Ⱌⴳ⒎⹰ⲧ⩷ₗ∸⬐⭹⓾✓⤓≪♽™⠒⌙➴⵶⡣⼷⬖⓭⒱Ȿ⹦⸪⼺⒣≕◼⾙‖");
#ifdef LINUX
tests.push_back(L"\\\\hello\\world!\\\\");
#endif // LINUX
std::wstring long_test;
for(int i = 0; i < 100; i++)
long_test += L"a";
tests.push_back(long_test);
for(int i = 0; i < tests.size(); i++)
{
while(!NSDirectory::Exists(input_dir + sep + tests[i]))
NSDirectory::CreateDirectories(input_dir + sep + tests[i]);
std::wstring dir = input_dir + sep + tests[i];
std::wstring file_path = dir + sep + tests[i];
std::wstring simple_file_path = dir + sep + L"file";
NSFile::CFileBinary file;
file.CreateFile(file_path);
file.WriteStringUTF8(file_path, true);
file.CloseFile();
file.CreateFile(simple_file_path);
file.WriteStringUTF8(simple_file_path, true);
file.CloseFile();
utils.CompressFileOrDirectory(input_dir + sep + tests[i], temp_dir + sep + tests[i] + L".zip");
}
std::vector<std::wstring> files = NSDirectory::GetFiles(temp_dir);
for(auto &file : files)
{
std::wstring filename = NSFile::GetFileName(file);
NSDirectory::CreateDirectory(output_dir + sep + filename);
utils.ExtractToDirectory(file, output_dir + sep + filename, NULL, false);
}
return 0;
}

View File

@ -0,0 +1,29 @@
QT -= core
QT -= gui
TARGET = tests
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
TEMPLATE = app
CORE_ROOT_DIR = $$PWD/../../
PWD_ROOT_DIR = $$PWD
SOURCES += \
$$PWD/../../Common/OfficeFileFormatChecker2.cpp \
$$PWD/../../Common/3dParty/pole/pole.cpp \
$$PWD/../../OOXML/Base/unicode_util.cpp
include($$CORE_ROOT_DIR/Common/base.pri)
include($$CORE_ROOT_DIR/Common/3dParty/icu/icu.pri)
DEFINES += PRO_DIR=\\\"$$PWD/\\\"
ADD_DEPENDENCY(kernel, graphics, UnicodeConverter)
SOURCES += main.cpp \
main.cpp
DESTDIR = $$PWD/build