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
4346ZipWriter::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
7080ZipWriter::~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
107135void
108136ZipWriter::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
200253private:
201254 std::string _zip_file_name;
202- void * _zip = nullptr ;
255+ // void* _zip = nullptr;
256+ unzFile _zip = nullptr ;
203257};
204258
205259ZipReader::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
225287ZipReader::~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
234297void 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
255326void
256327ZipReader::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 ());
0 commit comments