From 44ca145ff08044cffda7e03a04c8c18d8b78727f Mon Sep 17 00:00:00 2001 From: Enzo Evers Date: Fri, 27 Dec 2024 12:06:42 +0100 Subject: [PATCH] dev: Zip multiple files --- CoDeLib/CMakeLists.txt | 4 +- CoDeLib/Test/CMakeLists.txt | 2 + CoDeLib/Test/src/TestZipMinizip.c | 63 +++++++++++++++++++ CoDeLib/Test/src/TestZipMinizip.h | 3 + CoDeLib/Test/src/main.c | 3 + CoDeLib/Zip_minizip/CMakeLists.txt | 16 +++++ CoDeLib/Zip_minizip/src/Zip_minizip.c | 29 +++++++++ CoDeLib/include/CoDeLib/IZip.h | 26 ++++++++ .../include/CoDeLib/Zip_minizip/Zip_minizip.h | 5 ++ 9 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 CoDeLib/Test/src/TestZipMinizip.c create mode 100644 CoDeLib/Test/src/TestZipMinizip.h create mode 100644 CoDeLib/Zip_minizip/CMakeLists.txt create mode 100644 CoDeLib/Zip_minizip/src/Zip_minizip.c create mode 100644 CoDeLib/include/CoDeLib/IZip.h create mode 100644 CoDeLib/include/CoDeLib/Zip_minizip/Zip_minizip.h diff --git a/CoDeLib/CMakeLists.txt b/CoDeLib/CMakeLists.txt index 762b20b0..295f5273 100644 --- a/CoDeLib/CMakeLists.txt +++ b/CoDeLib/CMakeLists.txt @@ -20,6 +20,7 @@ set(COMMON_HEADERS ${CoDeLib_PUBLIC_INCLUDE_PATH}/IDeflate.h ${CoDeLib_PUBLIC_INCLUDE_PATH}/IInflate.h ${CoDeLib_PUBLIC_INCLUDE_PATH}/IUnZip.h + ${CoDeLib_PUBLIC_INCLUDE_PATH}/IZip.h ) install(FILES ${COMMON_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/CoDeLib) @@ -27,12 +28,13 @@ add_subdirectory(Deflate_zlib) add_subdirectory(Inflate_zlib) add_subdirectory(FileUtils) add_subdirectory(UnZip_minizip) +add_subdirectory(Zip_minizip) add_subdirectory(RaiiString) add_subdirectory(ZipContentInfo) add_subdirectory(Test) install( - TARGETS CoDeLib Deflate_zlib Inflate_zlib UnZip_minizip RaiiString ZipContentInfo FileUtils + TARGETS CoDeLib Deflate_zlib Inflate_zlib UnZip_minizip Zip_minizip RaiiString ZipContentInfo FileUtils EXPORT CoDeLibTargets INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} diff --git a/CoDeLib/Test/CMakeLists.txt b/CoDeLib/Test/CMakeLists.txt index 8b78df59..4ffce2be 100644 --- a/CoDeLib/Test/CMakeLists.txt +++ b/CoDeLib/Test/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(CoDeLib_Test src/TestDeflateInflateZlib.c src/TestFileUtils.c src/TestUnZipMinizip.c + src/TestZipMinizip.c src/TestUnZipMinizipInflateZlib.c src/TestZipContentInfo.c ) @@ -27,6 +28,7 @@ target_link_libraries(CoDeLib_Test PRIVATE Deflate_zlib) target_link_libraries(CoDeLib_Test PRIVATE Inflate_zlib) target_link_libraries(CoDeLib_Test PRIVATE FileUtils) target_link_libraries(CoDeLib_Test PRIVATE UnZip_minizip) +target_link_libraries(CoDeLib_Test PRIVATE Zip_minizip) target_link_libraries(CoDeLib_Test PRIVATE RaiiString) target_link_libraries(CoDeLib_Test PRIVATE ZipContentInfo) diff --git a/CoDeLib/Test/src/TestZipMinizip.c b/CoDeLib/Test/src/TestZipMinizip.c new file mode 100644 index 00000000..e69ad670 --- /dev/null +++ b/CoDeLib/Test/src/TestZipMinizip.c @@ -0,0 +1,63 @@ +#include "TestZipMinizip.h" +#include "unity_fixture.h" +#include +#include +#include +#include +#include +#include +#include + +static char *g_pFullPathToBenchmarkTestFiles; + +void SetupTestZipMinizip(char *pFullPathToBenchmarkTestFiles) { + g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles; +} + +TEST_GROUP(TestZipMinizip); + +static RaiiString g_pathToSmallBasicTextFileZipSource; +static RaiiString g_pathToMultiTextFileZipSource; +static RaiiString g_pathToMultiTextFileAndSubDirZipSource; + +TEST_SETUP(TestZipMinizip) { + g_pathToSmallBasicTextFileZipSource = + RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); + RaiiStringAppend_cString(&g_pathToSmallBasicTextFileZipSource, + "/SmallBasicTextFileZip/"); + + g_pathToMultiTextFileZipSource = + RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); + RaiiStringAppend_cString(&g_pathToMultiTextFileZipSource, + "/MultiTextFileZip/"); + + g_pathToMultiTextFileAndSubDirZipSource = + RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); + RaiiStringAppend_cString(&g_pathToMultiTextFileAndSubDirZipSource, + "/MultiTextFileAndSubDirZip/"); +} + +TEST_TEAR_DOWN(TestZipMinizip) { + RaiiStringClean(&g_pathToSmallBasicTextFileZipSource); + RaiiStringClean(&g_pathToMultiTextFileZipSource); + RaiiStringClean(&g_pathToMultiTextFileAndSubDirZipSource); + + if (PathExists("./tmp/")) { + TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/")); + } +} + +//============================== +// Zip() +//============================== + +TEST(TestZipMinizip, test_Zip_gfds) { TEST_IGNORE_MESSAGE("Implement me!"); } + +//============================== +// TEST_GROUP_RUNNER +//============================== + +TEST_GROUP_RUNNER(TestZipMinizip) { + // Zip() + RUN_TEST_CASE(TestZipMinizip, test_Zip_gfds); +} diff --git a/CoDeLib/Test/src/TestZipMinizip.h b/CoDeLib/Test/src/TestZipMinizip.h new file mode 100644 index 00000000..30abb2fb --- /dev/null +++ b/CoDeLib/Test/src/TestZipMinizip.h @@ -0,0 +1,3 @@ +#pragma once + +void SetupTestZipMinizip(char *pFullPathToBenchmarkTestFiles); diff --git a/CoDeLib/Test/src/main.c b/CoDeLib/Test/src/main.c index cfe9cf7d..e5397ff7 100644 --- a/CoDeLib/Test/src/main.c +++ b/CoDeLib/Test/src/main.c @@ -4,6 +4,7 @@ #include "TestFileUtils.h" #include "TestUnZipMinizip.h" #include "TestUnZipMinizipInflateZlib.h" +#include "TestZipMinizip.h" #include #include @@ -12,6 +13,7 @@ static void RunAllTests(void) { RUN_TEST_GROUP(TestDeflateInflateZlib); RUN_TEST_GROUP(TestFileUtils); RUN_TEST_GROUP(TestUnZipMinizip); + RUN_TEST_GROUP(TestZipMinizip); RUN_TEST_GROUP(TestUnZipMinizipInflateZlib); RUN_TEST_GROUP(TestZipContentInfo); } @@ -35,6 +37,7 @@ int main(int argc, const char **argv) { SetupTestFileUtils(fullPathToBenchmarkTestFiles.pString, currentWorkingDirectory.pString); SetupTestUnZipMinizip(fullPathToBenchmarkTestFiles.pString); + SetupTestZipMinizip(fullPathToBenchmarkTestFiles.pString); SetupTestUnZipMinizipInflateZlib(fullPathToBenchmarkTestFiles.pString); return UnityMain(argc, argv, RunAllTests); diff --git a/CoDeLib/Zip_minizip/CMakeLists.txt b/CoDeLib/Zip_minizip/CMakeLists.txt new file mode 100644 index 00000000..01a53c21 --- /dev/null +++ b/CoDeLib/Zip_minizip/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(Zip_minizip STATIC + src/Zip_minizip.c) + +target_include_directories(Zip_minizip PUBLIC + $ + $ +) + +find_package(MINIZIP REQUIRED) +target_link_libraries(Zip_minizip PRIVATE MINIZIP::minizip) + +set(Zip_minizip_PUBLIC_INCLUDE_PATH ${CoDeLib_PUBLIC_INCLUDE_PATH}/Zip_minizip) +set(Zip_minizip_PUBLIC_HEADERS + ${Zip_minizip_PUBLIC_INCLUDE_PATH}/Zip_minizip.h +) +install(FILES ${Zip_minizip_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/CoDeLib/Zip_minizip) diff --git a/CoDeLib/Zip_minizip/src/Zip_minizip.c b/CoDeLib/Zip_minizip/src/Zip_minizip.c new file mode 100644 index 00000000..2ae2995a --- /dev/null +++ b/CoDeLib/Zip_minizip/src/Zip_minizip.c @@ -0,0 +1,29 @@ +#include +#include + +// minizip +#include +#include + +//============================== +// Helper functions +//============================== + +//============================== +// Interface functions +//============================== + +ZIP_RETURN_CODES +Zip(const RaiiString *const pOutputZipPath __attribute__((unused)), + const RaiiString *const pInputPathArray __attribute__((unused)), + const size_t inputPathArraySize __attribute__((unused))) { + return ZIP_ERROR; +} + +const struct IZip zip_minizip = { + .Zip = Zip, +}; + +//============================== +// Helper functions +//============================== diff --git a/CoDeLib/include/CoDeLib/IZip.h b/CoDeLib/include/CoDeLib/IZip.h new file mode 100644 index 00000000..f580c84a --- /dev/null +++ b/CoDeLib/include/CoDeLib/IZip.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +typedef enum { ZIP_SUCCESS, ZIP_ERROR } ZIP_RETURN_CODES; + +struct IZip { + /*! + * @brief Zips the input file(s) and write the output to the output zip + * file. + * @param pOutputZipPath The path to the output zip file. If the directory + * does not exist, it will be created. + * @param pInputPathArray The array of paths to the files to be zipped. Both + * absolute and relative paths are supported. Path to directories will zip + * all files and sub-directories in the directory. Path to files will zip + * the file. The array can be a mix of absolute, relative, directies and + * files. + * @param inputPathArraySize The number of elements in pInputPathArray. + * @return ZIP_SUCCESS if the zipping was successful, ZIP_ERROR + * otherwise. + */ + ZIP_RETURN_CODES(*Zip) + (const RaiiString *const pOutputZipPath, + const RaiiString *const pInputPathArray, const size_t inputPathArraySize); +}; diff --git a/CoDeLib/include/CoDeLib/Zip_minizip/Zip_minizip.h b/CoDeLib/include/CoDeLib/Zip_minizip/Zip_minizip.h new file mode 100644 index 00000000..86f52414 --- /dev/null +++ b/CoDeLib/include/CoDeLib/Zip_minizip/Zip_minizip.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +extern const struct IZip zip_minizip;