Skip to content

Commit b5dc7af

Browse files
committed
Lets try zlib
Signed-off-by: Darby Johnston <[email protected]>
1 parent e4fb7d0 commit b5dc7af

File tree

10 files changed

+156
-58
lines changed

10 files changed

+156
-58
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "src/deps/zlib-ng"]
1414
path = src/deps/zlib-ng
1515
url = https://github.com/zlib-ng/zlib-ng.git
16+
[submodule "src/deps/zlib"]
17+
path = src/deps/zlib
18+
url = https://github.com/madler/zlib.git

CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ option(OTIO_INSTALL_PYTHON_MODULES "Install OTIO pure Python modules/files" O
3232
option(OTIO_INSTALL_COMMANDLINE_TOOLS "Install the OTIO command line tools" ON)
3333
option(OTIO_FIND_IMATH "Find Imath using find_package" OFF)
3434
option(OTIO_FIND_RAPIDJSON "Find RapidJSON using find_package" OFF)
35-
option(OTIO_FIND_MINIZIP_NG "Find minizip-ng using find_package" OFF)
35+
option(OTIO_FIND_ZLIB "Find zlib using find_package" OFF)
3636
set(OTIO_PYTHON_INSTALL_DIR "" CACHE STRING "Python installation dir (such as the site-packages dir)")
3737

3838
# Build options
@@ -257,14 +257,14 @@ else()
257257
message(STATUS "Using src/deps/rapidjson by default")
258258
endif()
259259

260-
#----- minizip-ng
261-
if(OTIO_FIND_MINIZIP_NG)
262-
find_package(minizip-ng CONFIG REQUIRED)
263-
if (minizip-ng_FOUND)
264-
message(STATUS "Found minizip-ng at ${minizip-ng_CONFIG}")
260+
#----- zlib
261+
if(OTIO_FIND_ZLIB)
262+
find_package(zlib CONFIG REQUIRED)
263+
if (zlib_FOUND)
264+
message(STATUS "Found zlib at ${zlib_CONFIG}")
265265
endif()
266266
else()
267-
message(STATUS "Using src/deps/minizip-ng by default")
267+
message(STATUS "Using src/deps/zlib by default")
268268
endif()
269269

270270
# set up the internally hosted dependencies

src/deps/CMakeLists.txt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ set(DEPS_SUBMODULES pybind11)
99
if(NOT OTIO_FIND_RAPIDJSON)
1010
set(DEPS_SUBMODULES ${DEPS_SUBMODULES} rapidjson)
1111
endif()
12-
if(NOT OTIO_FIND_MINIZIP_NG)
13-
set(DEPS_SUBMODULES ${DEPS_SUBMODULES} minizip-ng)
12+
if(NOT OTIO_FIND_ZLIB)
13+
set(DEPS_SUBMODULES ${DEPS_SUBMODULES} zlib)
1414
endif()
1515

1616
foreach(submodule IN LISTS DEPS_SUBMODULES)
@@ -33,23 +33,17 @@ if(NOT OTIO_FIND_IMATH)
3333
add_subdirectory(Imath)
3434
endif()
3535

36-
if(NOT OTIO_FIND_MINIZIP_NG)
36+
if(NOT OTIO_FIND_ZLIB)
3737
set(BUILD_SHARED_LIBS OFF)
3838
set(BUILD_TESTING OFF)
3939
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
40-
set(MZ_COMPAT ON)
41-
set(MZ_ZLIB ON)
42-
set(MZ_ZLIB_FLAVOR zlib-ng)
43-
set(MZ_BZIP2 OFF)
44-
set(MZ_LZMA OFF)
45-
set(MZ_ZSTD OFF)
46-
set(MZ_LIBCOMP OFF)
47-
set(MZ_PKCRYPT OFF)
48-
set(MZ_WZAES OFF)
49-
set(MZ_OPENSSL OFF)
50-
set(MZ_LIBBSD OFF)
51-
set(MZ_ICONV OFF)
52-
set(MZ_FETCH_LIBS OFF)
53-
add_subdirectory(zlib-ng)
54-
add_subdirectory(minizip-ng)
40+
set(ZLIB_BUILD_TESTING OFF)
41+
# \bug The zlib shared libraries are only enabled to workaround a bug in
42+
# the minizip CMake file. We still link to the static zlib.
43+
#set(ZLIB_BUILD_SHARED OFF)
44+
set(ZLIB_BUILD_MINIZIP ON)
45+
set(MINIZIP_BUILD_SHARED OFF)
46+
set(MINIZIP_BUILD_TESTING OFF)
47+
set(MINIZIP_ENABLE_BZIP2 OFF)
48+
add_subdirectory(zlib)
5549
endif()

src/deps/zlib

Submodule zlib added at 570720b

src/opentimelineio/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ endif()
107107

108108
target_link_libraries(opentimelineio
109109
PUBLIC opentime Imath::Imath
110-
PRIVATE MINIZIP::minizip)
110+
PRIVATE MINIZIP::minizipstatic ZLIB::ZLIBSTATIC)
111111

112112
set_target_properties(opentimelineio PROPERTIES
113113
DEBUG_POSTFIX "${OTIO_DEBUG_POSTFIX}"

src/opentimelineio/otioz.cpp

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include "opentimelineio/timeline.h"
99
#include "opentimelineio/urlUtils.h"
1010

11-
#include <mz.h>
11+
/*#include <mz.h>
1212
#include <mz_os.h>
1313
#include <mz_strm.h>
1414
#include <mz_zip.h>
15-
#include <mz_zip_rw.h>
15+
#include <mz_zip_rw.h>*/
16+
#include <unzip.h>
17+
#include <zip.h>
1618

1719
#include <fstream>
1820
#include <sstream>
@@ -36,13 +38,14 @@ class ZipWriter
3638
std::string const& file_name_in_zip);
3739

3840
private:
39-
void* _zip = nullptr;
40-
uint32_t _attributes = 0;
41+
//void* _zip = nullptr;
42+
//uint32_t _attributes = 0;
43+
zipFile _zip = nullptr;
4144
};
4245

4346
ZipWriter::ZipWriter(std::string const& zip_file_name)
4447
{
45-
_zip = mz_zip_writer_create();
48+
/*_zip = mz_zip_writer_create();
4649
if (!_zip)
4750
{
4851
std::stringstream ss;
@@ -64,15 +67,23 @@ ZipWriter::ZipWriter(std::string const& zip_file_name)
6467
std::stringstream ss;
6568
ss << "Cannot get file attributes: '" << zip_file_name << "'.";
6669
throw std::runtime_error(ss.str());
70+
}*/
71+
_zip = zipOpen64(zip_file_name.c_str(), 0);
72+
if (!_zip)
73+
{
74+
std::stringstream ss;
75+
ss << "Cannot create ZIP writer: '" << zip_file_name << "'.";
76+
throw std::runtime_error(ss.str());
6777
}
6878
}
6979

7080
ZipWriter::~ZipWriter()
7181
{
7282
if (_zip)
7383
{
74-
mz_zip_writer_close(_zip);
75-
mz_zip_writer_delete(&_zip);
84+
//mz_zip_writer_close(_zip);
85+
//mz_zip_writer_delete(&_zip);
86+
zipClose(_zip, nullptr);
7687
}
7788
}
7889

@@ -81,7 +92,7 @@ ZipWriter::add_compressed(
8192
std::string const& content,
8293
std::string const& file_name_in_zip)
8394
{
84-
mz_zip_file file_info;
95+
/*mz_zip_file file_info;
8596
memset(&file_info, 0, sizeof(mz_zip_file));
8697
mz_zip_writer_set_compress_level(_zip, MZ_COMPRESS_LEVEL_NORMAL);
8798
file_info.version_madeby = MZ_VERSION_MADEBY;
@@ -101,15 +112,32 @@ ZipWriter::add_compressed(
101112
std::stringstream ss;
102113
ss << "Cannot add file '" << file_name_in_zip << "' to ZIP.";
103114
throw std::runtime_error(ss.str());
104-
}
115+
}*/
116+
117+
zip_fileinfo zfi;
118+
memset(&zfi, 0, sizeof(zip_fileinfo));
119+
zipOpenNewFileInZip64(
120+
_zip,
121+
file_name_in_zip.c_str(),
122+
&zfi,
123+
nullptr,
124+
0,
125+
nullptr,
126+
0,
127+
nullptr,
128+
Z_DEFLATED,
129+
Z_DEFAULT_COMPRESSION,
130+
1);
131+
zipWriteInFileInZip(_zip, content.c_str(), (unsigned int)content.size());
132+
zipCloseFileInZip(_zip);
105133
}
106134

107135
void
108136
ZipWriter::add_uncompressed(
109137
std::filesystem::path const& path,
110138
std::string const& file_name_in_zip)
111139
{
112-
mz_zip_writer_set_compress_method(_zip, MZ_COMPRESS_METHOD_STORE);
140+
/*mz_zip_writer_set_compress_method(_zip, MZ_COMPRESS_METHOD_STORE);
113141
int32_t err = mz_zip_writer_add_file(
114142
_zip,
115143
path.u8string().c_str(),
@@ -119,7 +147,32 @@ ZipWriter::add_uncompressed(
119147
std::stringstream ss;
120148
ss << "Cannot add file '" << path.u8string() << "' to ZIP.";
121149
throw std::runtime_error(ss.str());
122-
}
150+
}*/
151+
152+
zip_fileinfo zfi;
153+
memset(&zfi, 0, sizeof(zip_fileinfo));
154+
zipOpenNewFileInZip64(
155+
_zip,
156+
file_name_in_zip.c_str(),
157+
&zfi,
158+
nullptr,
159+
0,
160+
nullptr,
161+
0,
162+
nullptr,
163+
0,
164+
0,
165+
1);
166+
FILE* f = fopen(path.u8string().c_str(), "rb");
167+
fseek(f, 0, SEEK_END);
168+
long size = ftell(f);
169+
fseek(f, 0, SEEK_SET);
170+
std::vector<uint8_t> buffer;
171+
buffer.resize(size);
172+
fread(buffer.data(), size, 1, f);
173+
fclose(f);
174+
zipWriteInFileInZip(_zip, buffer.data(), (unsigned int) buffer.size());
175+
zipCloseFileInZip(_zip);
123176
}
124177

125178
} // namespace
@@ -199,13 +252,14 @@ class ZipReader
199252

200253
private:
201254
std::string _zip_file_name;
202-
void* _zip = nullptr;
255+
//void* _zip = nullptr;
256+
unzFile _zip = nullptr;
203257
};
204258

205259
ZipReader::ZipReader(std::string const& zip_file_name)
206260
: _zip_file_name(zip_file_name)
207261
{
208-
_zip = mz_zip_reader_create();
262+
/*_zip = mz_zip_reader_create();
209263
if (!_zip)
210264
{
211265
std::stringstream ss;
@@ -219,21 +273,30 @@ ZipReader::ZipReader(std::string const& zip_file_name)
219273
std::stringstream ss;
220274
ss << "Cannot open ZIP file: '" << zip_file_name << "'.";
221275
throw std::runtime_error(ss.str());
276+
}*/
277+
278+
_zip = unzOpen64(zip_file_name.c_str());
279+
if (!_zip)
280+
{
281+
std::stringstream ss;
282+
ss << "Cannot create ZIP writer: '" << zip_file_name << "'.";
283+
throw std::runtime_error(ss.str());
222284
}
223285
}
224286

225287
ZipReader::~ZipReader()
226288
{
227289
if (_zip)
228290
{
229-
mz_zip_reader_close(_zip);
230-
mz_zip_reader_delete(&_zip);
291+
//mz_zip_reader_close(_zip);
292+
//mz_zip_reader_delete(&_zip);
293+
unzClose(_zip);
231294
}
232295
}
233296

234297
void ZipReader::extract(std::string const& file_name, std::string& text)
235298
{
236-
int32_t err = mz_zip_reader_locate_entry(_zip, file_name.c_str(), 0);
299+
/*int32_t err = mz_zip_reader_locate_entry(_zip, file_name.c_str(), 0);
237300
if (err != MZ_OK)
238301
{
239302
std::stringstream ss;
@@ -249,18 +312,61 @@ void ZipReader::extract(std::string const& file_name, std::string& text)
249312
std::stringstream ss;
250313
ss << "Cannot read file in ZIP: '" << file_name << "'.";
251314
throw std::runtime_error(ss.str());
252-
}
315+
}*/
316+
317+
unzLocateFile(_zip, file_name.c_str(), 0);
318+
unz_file_info64 ufi;
319+
unzGetCurrentFileInfo64(_zip, &ufi, nullptr, 0, nullptr, 0, nullptr, 0);
320+
unzOpenCurrentFile(_zip);
321+
text.resize(ufi.uncompressed_size);
322+
unzReadCurrentFile(_zip, text.data(), ufi.uncompressed_size);
323+
unzCloseCurrentFile(_zip);
253324
}
254325

255326
void
256327
ZipReader::extract_all(std::string const& output_dir)
257328
{
258-
int32_t err = mz_zip_reader_save_all(_zip, output_dir.c_str());
329+
/*int32_t err = mz_zip_reader_save_all(_zip, output_dir.c_str());
259330
if (err != MZ_OK)
260331
{
261332
std::stringstream ss;
262333
ss << "Cannot extract ZIP file: '" << _zip_file_name << "'.";
263334
throw std::runtime_error(ss.str());
335+
}*/
336+
337+
if (unzGoToFirstFile(_zip) == UNZ_OK)
338+
{
339+
do
340+
{
341+
unz_file_info64 ufi;
342+
std::string fileName;
343+
fileName.resize(1024);
344+
unzGetCurrentFileInfo64(
345+
_zip,
346+
&ufi,
347+
fileName.data(),
348+
fileName.size(),
349+
nullptr,
350+
0,
351+
nullptr,
352+
0);
353+
unzOpenCurrentFile(_zip);
354+
std::vector<uint8_t> buf(ufi.uncompressed_size);
355+
unzReadCurrentFile(_zip, buf.data(), ufi.uncompressed_size);
356+
unzCloseCurrentFile(_zip);
357+
const std::filesystem::path path =
358+
std::filesystem::u8path(output_dir)
359+
/ std::filesystem::u8path(fileName);
360+
const std::filesystem::path parentPath = path.parent_path();
361+
if (!std::filesystem::exists(parentPath))
362+
{
363+
std::filesystem::create_directory(parentPath);
364+
}
365+
FILE* f = fopen(path.u8string().c_str(), "wb");
366+
fwrite(buf.data(), buf.size(), 1, f);
367+
fclose(f);
368+
369+
} while (unzGoToNextFile(_zip) == UNZ_OK);
264370
}
265371
}
266372

@@ -290,6 +396,7 @@ from_otioz(
290396
<< "' exists, will not overwrite.";
291397
throw std::runtime_error(ss.str());
292398
}
399+
std::filesystem::create_directory(extract_path);
293400

294401
// Extract the archive.
295402
zip.extract_all(extract_path.u8string());

tests/CMakeLists.txt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ list(APPEND tests_opentime test_opentime)
1010
foreach(test ${tests_opentime})
1111
add_executable(${test} utils.h utils.cpp ${test}.cpp)
1212
target_link_libraries(${test} opentime)
13+
target_compile_definitions(${test} PUBLIC OTIO_TESTS_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
1314
set_target_properties(${test} PROPERTIES FOLDER tests)
14-
add_test(NAME ${test}
15-
COMMAND ${test}
16-
# Set the pwd to the source directory so we can load the samples
17-
# like the python tests do
18-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
15+
add_test(NAME ${test} COMMAND ${test})
1916
endforeach()
2017

2118
list(APPEND tests_opentimelineio
@@ -32,12 +29,8 @@ list(APPEND tests_opentimelineio
3229
test_otioz)
3330
foreach(test ${tests_opentimelineio})
3431
add_executable(${test} utils.h utils.cpp ${test}.cpp)
35-
3632
target_link_libraries(${test} opentimelineio)
33+
target_compile_definitions(${test} PUBLIC OTIO_TESTS_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
3734
set_target_properties(${test} PROPERTIES FOLDER tests)
38-
add_test(NAME ${test}
39-
COMMAND ${test}
40-
# Set the pwd to the source directory so we can load the samples
41-
# like the python tests do
42-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
35+
add_test(NAME ${test} COMMAND ${test})
4336
endforeach()

tests/test_otiod.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ main(int argc, char** argv)
2727

2828
// Sample data paths.
2929
std::filesystem::path const sample_data_dir =
30-
std::filesystem::current_path() / "sample_data";
30+
std::filesystem::u8path(OTIO_TESTS_DIR) / "sample_data";
3131
std::string const screening_example_path = otio::to_unix_separators(
3232
(sample_data_dir / "screening_example.otio").u8string());
3333

0 commit comments

Comments
 (0)