Skip to content

Commit

Permalink
dev: zip and unzip with minizip
Browse files Browse the repository at this point in the history
  • Loading branch information
enzoevers committed Dec 2, 2024
1 parent 3d69dd3 commit 9270f76
Show file tree
Hide file tree
Showing 48 changed files with 519 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ main.exe
Benchmark*.txt*
Benchmark/Files/*.compressed.*
Benchmark/Files/*.decompressed.*
Benchmark/Files/*.zipped.*
Benchmark/Files/*.unzipped.*

# Test generated files
Test*.txt*
Expand Down
21 changes: 20 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@
"inflate_zlib.h": "c",
"unity.h": "c",
"raiistring.h": "c",
"fileutils.h": "c"
"fileutils.h": "c",
"zip_minizip.h": "c",
"unzip.h": "c",
"unzip_minizip.h": "c",
"testzipunzipminizip.h": "c",
"unity_fixture.h": "c",
"testdeflateinflatezlib.h": "c",
"stdbool.h": "c",
"cstdlib": "c",
"rope": "c",
"*.def": "c",
"string.h": "c",
"testunzipminizip.h": "c",
"unity_fixture_internals.h": "c",
"unity_memory.h": "c",
"string_view": "c",
"regex": "c",
"*.inc": "c",
"bitset": "c",
"zipcontentinfo.h": "c"
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is
the first text file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Second file is this
.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is in a directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The 3rd file contains

an empty line on line 2.
Binary file not shown.
1 change: 1 addition & 0 deletions Benchmark/Files/MultiTextFileAndSubDirZip_store/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The 3rd file contains

an empty line on line 2.
Binary file not shown.
1 change: 1 addition & 0 deletions Benchmark/Files/MultiTextFileZip_store/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the files.
2 changes: 2 additions & 0 deletions Benchmark/Files/MultiTextFileZip_store/TextFileOne_store.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is
the first text file.
Empty file.
2 changes: 2 additions & 0 deletions Benchmark/Files/MultiTextFileZip_store/TextFileTwo_store.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Second file is this
.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The 3rd file contains

an empty line on line 2.
Empty file.
1 change: 1 addition & 0 deletions Benchmark/Files/SmallBasicTextFileZip_store/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a test file.
On the second line there are eight words
The third line has a tab.
And the last line is a longer piece. Consising of two sentences.
Empty file.
Binary file not shown.
7 changes: 6 additions & 1 deletion CoDeLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ set(CoDeLib_PUBLIC_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include/CoDeLib)
set(COMMON_HEADERS
${CoDeLib_PUBLIC_INCLUDE_PATH}/IDeflate.h
${CoDeLib_PUBLIC_INCLUDE_PATH}/IInflate.h
${CoDeLib_PUBLIC_INCLUDE_PATH}/IZip.h
${CoDeLib_PUBLIC_INCLUDE_PATH}/IUnZip.h
)
install(FILES ${COMMON_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/CoDeLib)

add_subdirectory(Deflate_zlib)
add_subdirectory(Inflate_zlib)
add_subdirectory(Zip_minizip)
add_subdirectory(UnZip_minizip)
add_subdirectory(RaiiString)
add_subdirectory(ZipContentInfo)
add_subdirectory(Test)

install(
TARGETS CoDeLib Deflate_zlib Inflate_zlib RaiiString Utility
TARGETS CoDeLib Deflate_zlib Inflate_zlib Zip_minizip UnZip_minizip RaiiString ZipContentInfo Utility
EXPORT CoDeLibTargets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand Down
4 changes: 3 additions & 1 deletion CoDeLib/Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ check_required_components(CoDeLib)
include(CMakeFindDependencyMacro)

set(ZLIB_USE_STATIC_LIBS "ON")
find_dependency(ZLIB REQUIRED)
find_dependency(ZLIB REQUIRED)

find_dependency(minizip REQUIRED)
3 changes: 2 additions & 1 deletion CoDeLib/CustomDeflate.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

# refs

- https://pnrsolution.org/Datacenter/Vol4/Issue1/58.pdf
- https://pnrsolution.org/Datacenter/Vol4/Issue1/58.pdf
- https://nachtimwald.com/2019/09/08/making-minizip-easier-to-use/
1 change: 0 additions & 1 deletion CoDeLib/Deflate_zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ set(ZLIB_USE_STATIC_LIBS "ON")
find_package(ZLIB REQUIRED)
target_link_libraries(Deflate_zlib PRIVATE ZLIB::ZLIB)

message(STATUS "CoDeLib_PUBLIC_INCLUDE_PATH: ${CoDeLib_PUBLIC_INCLUDE_PATH}")
set(Deflate_zlib_PUBLIC_INCLUDE_PATH ${CoDeLib_PUBLIC_INCLUDE_PATH}/Deflate_zlib)
set(Deflate_zlib_PUBLIC_HEADERS
${Deflate_zlib_PUBLIC_INCLUDE_PATH}/Deflate_zlib.h
Expand Down
2 changes: 1 addition & 1 deletion CoDeLib/RaiiString/src/RaiiString.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ void RaiiStringClean(RaiiString *pThis) {
pThis->pString = NULL;
}
pThis->lengthWithTermination = 0;
}
}
21 changes: 15 additions & 6 deletions CoDeLib/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
add_executable(CoDeLib_Test
src/main.c
src/TestRaiiString.c
src/TestDeflateInflateZlib.c)
src/TestDeflateInflateZlib.c
src/TestUnZipMinizip.c
src/TestZipContentInfo.c
)

target_include_directories(CoDeLib_Test PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)

target_link_libraries(CoDeLib_Test PRIVATE Deflate_zlib)
target_link_libraries(CoDeLib_Test PRIVATE Inflate_zlib)
target_link_libraries(CoDeLib_Test PRIVATE RaiiString)

FetchContent_Declare(
Unity
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
Expand All @@ -21,7 +20,17 @@ FetchContent_Declare(
# If cache is not used, you will get `Policy CMP0077 is not set: option() honors normal variables.`
set(UNITY_EXTENSION_FIXTURE ON CACHE INTERNAL "")
FetchContent_MakeAvailable(Unity)
target_link_libraries(CoDeLib_Test PRIVATE unity)

add_subdirectory(Utility)
target_link_libraries(CoDeLib_Test PUBLIC Utility)

target_link_libraries(CoDeLib_Test PRIVATE Deflate_zlib)
target_link_libraries(CoDeLib_Test PRIVATE Inflate_zlib)
target_link_libraries(CoDeLib_Test PRIVATE Zip_minizip)
target_link_libraries(CoDeLib_Test PRIVATE UnZip_minizip)
target_link_libraries(CoDeLib_Test PRIVATE RaiiString)
target_link_libraries(CoDeLib_Test PRIVATE ZipContentInfo)

target_link_libraries(CoDeLib_Test PRIVATE unity)


2 changes: 1 addition & 1 deletion CoDeLib/Test/src/TestDeflateInflateZlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ TEST(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib) {

TEST_GROUP_RUNNER(TestDeflateInflateZlib) {
RUN_TEST_CASE(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib);
}
}
2 changes: 1 addition & 1 deletion CoDeLib/Test/src/TestRaiiString.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ TEST_GROUP_RUNNER(TestRaiiString) {
// RaiiStringClean()
RUN_TEST_CASE(TestRaiiString, test_RaiiStringClean_SetsNullptrInObject);
RUN_TEST_CASE(TestRaiiString, test_RaiiStringClean_SetsLengthZeroInObject);
}
}
88 changes: 88 additions & 0 deletions CoDeLib/Test/src/TestUnZipMinizip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "unity_fixture.h"
#include <CoDeLib/RaiiString/RaiiString.h>
#include <CoDeLib/Test/Utility/FileUtils.h>
#include <CoDeLib/UnZip_minizip/UnZip_minizip.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>

static char *g_pFullPathToBenchmarkTestFiles = NULL;

void SetupTestUnZipMinizip(char *pFullPathToBenchmarkTestFiles) {
g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles;
}

TEST_GROUP(TestUnZipMinizip);

TEST_SETUP(TestUnZipMinizip) {
// Nothing
}

TEST_TEAR_DOWN(TestUnZipMinizip) {
// Nothing
}

TEST(TestUnZipMinizip, test_UnZipMinizip_UnZipsSingleFileCorrectly) {
TEST_IGNORE_MESSAGE("In progress");

FILE *pInFile = NULL;
FILE *pUnZippedFile = NULL;
FILE *pReferenceUnZippedFile = NULL;

OpenFile(&pInFile, g_pFullPathToBenchmarkTestFiles,
"/SmallBasicTextFileZip_store/SmallBasicTextFile_store.zip", "r");
OpenFile(
&pUnZippedFile, g_pFullPathToBenchmarkTestFiles,
"/SmallBasicTextFileZip_store/SmallBasicTextFile_store.unzipped.txt",
"w+");
OpenFile(&pReferenceUnZippedFile, g_pFullPathToBenchmarkTestFiles,
"/SmallBasicTextFileZip_store/SmallBasicTextFile_store.txt", "r");

TEST_ASSERT(FilesAreEqual(pUnZippedFile, pReferenceUnZippedFile));
fclose(pInFile);
fclose(pUnZippedFile);
fclose(pReferenceUnZippedFile);
}

TEST(TestUnZipMinizip, test_UnZipMinizip_UnZipsMultipleFilesCorrectly) {
TEST_IGNORE_MESSAGE("In progress");

FILE *pInFile = NULL;
FILE *pUnZippedFiles[] = {NULL, NULL, NULL};
FILE *pReferenceUnZippedFiles[] = {NULL, NULL, NULL};
const size_t fileCount = 3;

OpenFile(&pInFile, g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/MultiTextFileZip_store.zip", "r");
// Output files
OpenFile(&pUnZippedFiles[0], g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/TextFileOne_store.unzipped.txt", "w+");
OpenFile(&pUnZippedFiles[1], g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/TextFileTwo_store.unzipped.txt", "w+");
OpenFile(&pUnZippedFiles[2], g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/ThirdTextFile_store.unzipped.txt", "w+");
// Reference files
OpenFile(&pReferenceUnZippedFiles[0], g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/TextFileOne_store.txt", "r");
OpenFile(&pReferenceUnZippedFiles[1], g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/TextFileTwo_store.txt", "r");
OpenFile(&pReferenceUnZippedFiles[2], g_pFullPathToBenchmarkTestFiles,
"/MultiTextFileZip_store/ThirdTextFile_store.txt", "r");

for (size_t i = 0; i < fileCount; ++i) {
TEST_ASSERT(
FilesAreEqual(pUnZippedFiles[i], pReferenceUnZippedFiles[i]));
}
fclose(pInFile);
for (size_t i = 0; i < fileCount; ++i) {
fclose(pUnZippedFiles[i]);
fclose(pReferenceUnZippedFiles[i]);
}
}

TEST_GROUP_RUNNER(TestUnZipMinizip) {
RUN_TEST_CASE(TestUnZipMinizip,
test_UnZipMinizip_UnZipsSingleFileCorrectly);
RUN_TEST_CASE(TestUnZipMinizip,
test_UnZipMinizip_UnZipsMultipleFilesCorrectly);
}
3 changes: 3 additions & 0 deletions CoDeLib/Test/src/TestUnZipMinizip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void SetupTestUnZipMinizip(char *pFullPathToBenchmarkTestFiles);
68 changes: 68 additions & 0 deletions CoDeLib/Test/src/TestZipContentInfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "unity_fixture.h"
#include <CoDeLib/ZipContentInfo/ZipContentInfo.h>
#include <assert.h>
#include <string.h>

TEST_GROUP(TestZipContentInfo);

static ZipContentInfo zipContentInfo;

TEST_SETUP(TestZipContentInfo) {}

TEST_TEAR_DOWN(TestZipContentInfo) { ZipContentInfoClean(&zipContentInfo); }

//==============================
// ZipContentInfoClean()
//==============================

TEST(
TestZipContentInfo,
test_ZipContentInfoClean_SetsZipFileNameToNullptrIfThereIsNoFileNameArray) {

zipContentInfo =
(ZipContentInfo){.name = RaiiStringCreateFromCString("myZip.zip"),
.pFileArray = NULL,
.fileCount = 0};

ZipContentInfoClean(&zipContentInfo);

TEST_ASSERT_NULL(zipContentInfo.name.pString);
TEST_ASSERT_NULL(zipContentInfo.pFileArray);
TEST_ASSERT_EQUAL(0, zipContentInfo.fileCount);
}

TEST(TestZipContentInfo, test_ZipContentInfoClean_CleansAllFileNamesInArray) {

RaiiString fileNames[] = {
RaiiStringCreateFromCString("file1.txt"),
RaiiStringCreateFromCString("SomeDir/file2.txt"),
RaiiStringCreateFromCString("file3.txt"),
};
zipContentInfo =
(ZipContentInfo){.name = RaiiStringCreateFromCString("myZip.zip"),
.pFileArray = &fileNames[0],
.fileCount = 3};

ZipContentInfoClean(&zipContentInfo);

TEST_ASSERT_NULL(zipContentInfo.name.pString);
TEST_ASSERT_NULL(zipContentInfo.pFileArray);
TEST_ASSERT_EQUAL(0, zipContentInfo.fileCount);

for (size_t i = 0; i < 3; i++) {
TEST_ASSERT_NULL(fileNames[i].pString);
}
}

//==============================
// TEST_GROUP_RUNNER
//==============================

TEST_GROUP_RUNNER(TestZipContentInfo) {
// ZipContentInfoClean()
RUN_TEST_CASE(
TestZipContentInfo,
test_ZipContentInfoClean_SetsZipFileNameToNullptrIfThereIsNoFileNameArray);
RUN_TEST_CASE(TestZipContentInfo,
test_ZipContentInfoClean_CleansAllFileNamesInArray);
}
4 changes: 4 additions & 0 deletions CoDeLib/Test/src/main.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "unity_fixture.h"

#include "TestDeflateInflateZlib.h"
#include "TestUnZipMinizip.h"
#include <CoDeLib/RaiiString/RaiiString.h>

static void RunAllTests(void) {
RUN_TEST_GROUP(TestRaiiString);
RUN_TEST_GROUP(TestDeflateInflateZlib);
RUN_TEST_GROUP(TestUnZipMinizip);
RUN_TEST_GROUP(TestZipContentInfo);
}

int main(int argc, const char **argv) {
Expand All @@ -21,6 +24,7 @@ int main(int argc, const char **argv) {
}

SetupTestDeflateInflateZlib(fullPathToBenchmarkTestFiles.pString);
SetupTestUnZipMinizip(fullPathToBenchmarkTestFiles.pString);

return UnityMain(argc, argv, RunAllTests);
}
Loading

0 comments on commit 9270f76

Please sign in to comment.