├── .gitignore ├── CMakeLists.txt ├── LICENSE.md ├── README.md ├── examples ├── pipe.cpp ├── read.cpp └── write.cpp ├── tests └── test.cpp └── zip_file.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | build/ 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2) 2 | project(miniz-cpp) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 6 | 7 | set(TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/tests/test.cpp) 8 | 9 | add_executable(miniz-cpp.test ${TEST_SOURCE}) 10 | target_include_directories(miniz-cpp.test 11 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) 12 | 13 | set(EXAMPLE_SOURCES 14 | ${CMAKE_CURRENT_SOURCE_DIR}/examples/pipe.cpp 15 | ${CMAKE_CURRENT_SOURCE_DIR}/examples/read.cpp 16 | ${CMAKE_CURRENT_SOURCE_DIR}/examples/write.cpp) 17 | 18 | foreach(EXAMPLE_SOURCE IN ITEMS ${EXAMPLE_SOURCES}) 19 | get_filename_component(EXAMPLE_NAME ${EXAMPLE_SOURCE} NAME_WE) 20 | set(EXAMPLE_EXECUTABLE example-${EXAMPLE_NAME}) 21 | 22 | add_executable(${EXAMPLE_EXECUTABLE} ${EXAMPLE_SOURCE}) 23 | 24 | target_include_directories(${EXAMPLE_EXECUTABLE} 25 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) 26 | endforeach() 27 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2017 Thomas Fussell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | miniz-cpp 2 | ========= 3 | 4 | A cross-platform header-only library for reading and writing ZIP files using a nice simple API similar to [Python's zipfile](https://docs.python.org/3/library/zipfile.html). See [examples/](https://github.com/tfussell/miniz-cpp/tree/master/examples) for a demonstration of the use of the library. -------------------------------------------------------------------------------- /examples/pipe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // Platform-specific includes 7 | #ifdef _WIN32 8 | #include // For O_BINARY 9 | #include // For _setmode, _fileno, _isatty 10 | #else // POSIX 11 | #include // For fileno, isatty 12 | #endif 13 | 14 | #include 15 | 16 | namespace { 17 | 18 | // Returns true if program is being run directly 19 | // If this is false, we assume that input will be given through stdin 20 | bool is_tty() 21 | { 22 | #ifdef _WIN32 23 | return _isatty(_fileno(stdin)) != 0; 24 | #else 25 | return isatty(fileno(stdin)) != 0; 26 | #endif 27 | } 28 | 29 | } // namespace 30 | 31 | // Either 1. Print directory structure of zip file given as filename or piped through stdin 32 | // or 2. Print contents of file contained in aforementioned zip file 33 | int main(int argc, char *argv[]) 34 | { 35 | miniz_cpp::zip_file file; 36 | std::vector args; 37 | 38 | for(int i = 1; i < argc; i++) 39 | { 40 | args.push_back(argv[i]); 41 | } 42 | 43 | if(is_tty()) // Expect first argument to be zip file path 44 | { 45 | if(args.size() < 1) 46 | { 47 | std::cout << "usage: " << argv[0] << " zip_file [file_to_print]" << std::endl; 48 | std::cout << " (zip_file can be replaced by data piped from standard input)" << std::endl; 49 | return 1; 50 | } 51 | 52 | file.load(args.front()); 53 | args.erase(args.begin()); 54 | } 55 | else 56 | { 57 | #ifdef _WIN32 58 | // Ensure that we are reading the raw binary data when using a pipe in Windows. 59 | _setmode(_fileno(stdin), O_BINARY); 60 | #endif 61 | file.load(std::cin); 62 | } 63 | 64 | if(args.empty()) 65 | { 66 | file.printdir(); 67 | } 68 | else 69 | { 70 | std::cout << file.read(args.front()) << std::endl; 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /examples/read.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | if(argc <= 1) 7 | { 8 | std::cout << "usage: " << argv[0] << " filename" << std::endl; 9 | return 0; 10 | } 11 | 12 | miniz_cpp::zip_file file(argv[1]); 13 | file.printdir(); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /examples/write.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | if(argc <= 1) 7 | { 8 | std::cout << "usage: " << argv[0] << " filename" << std::endl; 9 | return 0; 10 | } 11 | 12 | miniz_cpp::zip_file file; 13 | 14 | file.writestr("file1.txt", "this is file 1"); 15 | file.writestr("file2.txt", "this is file 2"); 16 | file.writestr("file3.txt", "this is file 3"); 17 | file.writestr("file4.txt", "this is file 4"); 18 | file.writestr("file5.txt", "this is file 5"); 19 | 20 | file.save(argv[1]); 21 | 22 | return 0; 23 | } 24 | --------------------------------------------------------------------------------