Skip to content

Commit

Permalink
dev: Zip multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
enzoevers committed Jan 19, 2025
1 parent 5e35ee4 commit 09787ac
Show file tree
Hide file tree
Showing 17 changed files with 1,259 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CoDeLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ 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)

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}
Expand Down
5 changes: 4 additions & 1 deletion CoDeLib/CustomDeflate.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# refs

- https://pnrsolution.org/Datacenter/Vol4/Issue1/58.pdf
- https://nachtimwald.com/2019/09/08/making-minizip-easier-to-use/
- https://nachtimwald.com/2019/09/08/making-minizip-easier-to-use/
- https://pkwaredownloads.blob.core.windows.net/pkware-general/Documentation/APPNOTE-6.3.9.TXT
- https://github.com/zlib-ng/minizip-ng/blob/cf5404bb714ee11ca2fdc3168d9770a11ec8b576/test/fuzz/zip_fuzzer.c#L100
- https://stackoverflow.com/questions/12609747/traversing-a-filesystem-with-fts3
3 changes: 1 addition & 2 deletions CoDeLib/RaiiString/src/RaiiString.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ RaiiString RaiiStringCreateFromCString(const char *pCString) {

RaiiString newRaiistring = {NULL, 0};

if (lengthWithoutTermination == 0 ||
lengthWithoutTermination == MAX_CSTRING_INCLUDING_TERMINATION_LENGTH) {
if (lengthWithoutTermination == MAX_CSTRING_INCLUDING_TERMINATION_LENGTH) {
return newRaiistring;
}

Expand Down
3 changes: 3 additions & 0 deletions CoDeLib/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ add_executable(CoDeLib_Test
src/TestDeflateInflateZlib.c
src/TestFileUtils.c
src/TestUnZipMinizip.c
src/TestZipMinizip.c
src/TestUnZipMinizipInflateZlib.c
src/TestZipMinizipUnZipMinizip.c
src/TestZipContentInfo.c
)

Expand All @@ -27,6 +29,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)

Expand Down
8 changes: 4 additions & 4 deletions CoDeLib/Test/src/TestRaiiString.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ TEST(

TEST(
TestRaiiString,
test_RaiiStringCreateFromCString_SetsLengthOfZeroAndNullptrIfProvidedEmptyString) {
test_RaiiStringCreateFromCString_SetsExpectedLengthAndPtrIfProvidedEmptyString) {
const char *pCString = "";
raiiString = RaiiStringCreateFromCString(pCString);
TEST_ASSERT_NULL(raiiString.pString);
TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination);
TEST_ASSERT_NOT_NULL(raiiString.pString);
TEST_ASSERT_EQUAL(1, raiiString.lengthWithTermination);
}

//==============================
Expand Down Expand Up @@ -297,7 +297,7 @@ TEST_GROUP_RUNNER(TestRaiiString) {
test_RaiiStringCreateFromCString_SetsZeroLengthAndNullptrIfStringLengthIsGreaterThanMaxTotalLength);
RUN_TEST_CASE(
TestRaiiString,
test_RaiiStringCreateFromCString_SetsLengthOfZeroAndNullptrIfProvidedEmptyString);
test_RaiiStringCreateFromCString_SetsExpectedLengthAndPtrIfProvidedEmptyString);

// RaiiStringClean()
RUN_TEST_CASE(TestRaiiString,
Expand Down
4 changes: 4 additions & 0 deletions CoDeLib/Test/src/TestUnZipMinizip.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ static RaiiString g_pathToMultiTextFileAndSubDirZipStore;
static RaiiString g_pathToMultiTextFileAndSubDirZipSource;

TEST_SETUP(TestUnZipMinizip) {
if (PathExists("./tmp/")) {
TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/"));
}

g_someUnZippedDirPath =
RaiiStringCreateFromCString("SomePath/someUnZippedDirPath.zip");
g_someZipPath = RaiiStringCreateFromCString("SomePath/someZipPath.zip");
Expand Down
4 changes: 4 additions & 0 deletions CoDeLib/Test/src/TestUnZipMinizipInflateZlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ static RaiiString g_pathToMultiTextFileAndSubDirZipDeflate;
static RaiiString g_pathToMultiTextFileAndSubDirZipSource;

TEST_SETUP(TestUnZipMinizipInflateZlib) {
if (PathExists("./tmp/")) {
TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/"));
}

g_someUnZippedDirPath =
RaiiStringCreateFromCString("SomePath/someUnZippedDirPath.zip");
g_someZipPath = RaiiStringCreateFromCString("SomePath/someZipPath.zip");
Expand Down
180 changes: 180 additions & 0 deletions CoDeLib/Test/src/TestZipMinizip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#include "TestZipMinizip.h"
#include "unity_fixture.h"
#include <CoDeLib/FileUtils/FileUtils.h>
#include <CoDeLib/RaiiString/RaiiString.h>
#include <CoDeLib/Zip_minizip/Zip_minizip.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

static char *g_pFullPathToBenchmarkTestFiles;

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

TEST_GROUP(TestZipMinizip);

static RaiiString g_pathToSmallBasicTextFileZipDeflate;
static RaiiString g_pathToSmallBasicTextFileZipSource;
static RaiiString g_pathToMultiTextFileZipSource;
static RaiiString g_pathToMultiTextFileAndSubDirZipSource;

TEST_SETUP(TestZipMinizip) {
if (PathExists("./tmp/")) {
TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/"));
}

g_pathToSmallBasicTextFileZipDeflate =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToSmallBasicTextFileZipDeflate,
"/SmallBasicTextFileZip/"
"SmallBasicTextFile_deflate.zip");

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_pathToSmallBasicTextFileZipDeflate);
RaiiStringClean(&g_pathToSmallBasicTextFileZipSource);
RaiiStringClean(&g_pathToMultiTextFileZipSource);
RaiiStringClean(&g_pathToMultiTextFileAndSubDirZipSource);

if (PathExists("./tmp/")) {
TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/"));
}
}

//==============================
// Zip()
//==============================

TEST(TestZipMinizip, test_Zip_ReturnsErrorIfOutputZipPathIsNullptr) {
const char *pInputPathArray[] = {
"123",
"asdf",
};

const ZIP_RETURN_CODES statusZip =
zip_minizip.Zip(NULL, &pInputPathArray[0], 2);
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip);
}

TEST(TestZipMinizip, test_Zip_ReturnsErrorIfInputPathArrayIsNullptr) {
const char dummyString[] = "123";

const ZIP_RETURN_CODES statusZip =
zip_minizip.Zip(&dummyString[0], NULL, 2);
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip);
}

TEST(TestZipMinizip, test_Zip_ReturnsErrorIfProvidedSizeIsZero) {
const char dummyString[] = "123";
const char *pInputPathArray[] = {
"123",
"asdf",
};

const ZIP_RETURN_CODES statusZip =
zip_minizip.Zip(&dummyString[0], &pInputPathArray[0], 0);
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip);
}

TEST(TestZipMinizip, test_Zip_ReturnsErrorIfZipFileAlreadyExists) {
RAII_STRING dummyZipPath = RaiiStringCreateFromCString("./tmp/");
RecursiveMkdir(dummyZipPath.pString);

RaiiStringAppend_cString(&dummyZipPath, "SomeZip.zip");
FILE *pDummyFile = fopen(dummyZipPath.pString, "w");
TEST_ASSERT_NOT_NULL(pDummyFile);
fclose(pDummyFile);

const char *pInputPathArray[] = {
"123",
};

const ZIP_RETURN_CODES statusZip =
zip_minizip.Zip(dummyZipPath.pString, &pInputPathArray[0], 1);
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip);
}

TEST(TestZipMinizip,
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingRelativePath) {
RAII_STRING inputTextFilePath = RaiiStringCreateFromCString(
g_pathToSmallBasicTextFileZipSource.pString);
RaiiStringAppend_cString(&inputTextFilePath, "SmallBasicTextFile.txt");

const char *pInputPathArray[] = {
inputTextFilePath.pString,
};

RAII_STRING outputZipPath =
RaiiStringCreateFromCString("./tmp/SmallBasicTextFile.zip");

TEST_ASSERT_FALSE(PathExists(outputZipPath.pString));

const ZIP_RETURN_CODES statusZip =
zip_minizip.Zip(outputZipPath.pString, &pInputPathArray[0], 1);
TEST_ASSERT_EQUAL(ZIP_SUCCESS, statusZip);

TEST_ASSERT_TRUE(PathExists(outputZipPath.pString));
}

TEST(TestZipMinizip,
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingAbsolutePath) {
RAII_STRING inputTextFilePath = RaiiStringCreateFromCString(
g_pathToSmallBasicTextFileZipSource.pString);
RaiiStringAppend_cString(&inputTextFilePath, "SmallBasicTextFile.txt");

const char *pInputPathArray[] = {
inputTextFilePath.pString,
};

char cwdBuffer[MAX_PATH_LENGTH_WTH_TERMINATOR];
GetCurrentWorkingDirectory(&cwdBuffer[0], MAX_PATH_LENGTH_WTH_TERMINATOR);

RAII_STRING outputZipPath = RaiiStringCreateFromCString(&cwdBuffer[0]);
RaiiStringAppend_cString(&outputZipPath, "tmp/SmallBasicTextFile.zip");

TEST_ASSERT_FALSE(PathExists(outputZipPath.pString));

const ZIP_RETURN_CODES statusZip =
zip_minizip.Zip(outputZipPath.pString, &pInputPathArray[0], 1);
TEST_ASSERT_EQUAL(ZIP_SUCCESS, statusZip);

TEST_ASSERT_TRUE(PathExists(outputZipPath.pString));
}

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

TEST_GROUP_RUNNER(TestZipMinizip) {
// Zip()
RUN_TEST_CASE(TestZipMinizip,
test_Zip_ReturnsErrorIfOutputZipPathIsNullptr);
RUN_TEST_CASE(TestZipMinizip,
test_Zip_ReturnsErrorIfInputPathArrayIsNullptr);
RUN_TEST_CASE(TestZipMinizip, test_Zip_ReturnsErrorIfProvidedSizeIsZero);
RUN_TEST_CASE(TestZipMinizip, test_Zip_ReturnsErrorIfZipFileAlreadyExists);
RUN_TEST_CASE(
TestZipMinizip,
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingRelativePath);
RUN_TEST_CASE(
TestZipMinizip,
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingAbsolutePath);
}
3 changes: 3 additions & 0 deletions CoDeLib/Test/src/TestZipMinizip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void SetupTestZipMinizip(char *pFullPathToBenchmarkTestFiles);
Loading

0 comments on commit 09787ac

Please sign in to comment.