Skip to content

Commit a1e8dd5

Browse files
Merge pull request #1141 from Geode-solutions/fix/do_not_create_empty_directory_if_zip_fails
fix(ZipFile): Remove the created empty directory if zip file fails.
2 parents 94fb0e4 + f8ff97c commit a1e8dd5

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

src/geode/basic/zip_file.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ namespace geode
6161
writer_, MZ_COMPRESS_METHOD_STORE );
6262
const auto status = mz_zip_writer_open_file(
6363
writer_, to_string( file ).c_str(), 0, 0 );
64-
OPENGEODE_EXCEPTION(
65-
status == MZ_OK, "[ZipFile] Error opening zip for writing." );
64+
if( status != MZ_OK )
65+
{
66+
std::filesystem::remove_all( directory_ );
67+
throw OpenGeodeException(
68+
"[ZipFile] Error opening zip for writing." );
69+
}
6670
}
6771

6872
~Impl()
6973
{
70-
std::filesystem::remove( directory_ );
74+
std::filesystem::remove_all( directory_ );
7175
const auto status = mz_zip_writer_close( writer_ );
7276
if( status != MZ_OK )
7377
{
@@ -89,8 +93,12 @@ namespace geode
8993
const std::filesystem::path file_path{ to_string( file ) };
9094
const auto status = mz_zip_writer_add_path(
9195
writer_, file_path.string().c_str(), nullptr, 0, 1 );
92-
OPENGEODE_EXCEPTION( status == MZ_OK,
93-
"[ZipFile::archive_file] Error adding path to zip" );
96+
if( status != MZ_OK )
97+
{
98+
std::filesystem::remove_all( directory_ );
99+
throw OpenGeodeException(
100+
"[ZipFile::archive_file] Error adding path to zip" );
101+
}
94102
std::filesystem::remove( file_path );
95103
}
96104

@@ -137,8 +145,12 @@ namespace geode
137145
reader_ = mz_zip_reader_create();
138146
const auto status =
139147
mz_zip_reader_open_file( reader_, to_string( file ).c_str() );
140-
OPENGEODE_EXCEPTION(
141-
status == MZ_OK, "[UnzipFile] Error opening zip for reading" );
148+
if( status != MZ_OK )
149+
{
150+
std::filesystem::remove_all( directory_ );
151+
throw OpenGeodeException(
152+
"[UnzipFile] Error opening zip for reading" );
153+
}
142154
}
143155

144156
~Impl()
@@ -155,15 +167,23 @@ namespace geode
155167
{
156168
mz_zip_file* file_info{ nullptr };
157169
status = mz_zip_reader_entry_get_info( reader_, &file_info );
158-
OPENGEODE_EXCEPTION( status == MZ_OK, "[UnzipFile::extract_all]"
159-
" Error getting entry "
160-
"info in zip file" );
170+
if( status != MZ_OK )
171+
{
172+
std::filesystem::remove_all( directory_ );
173+
throw OpenGeodeException( "[UnzipFile::extract_all] Error "
174+
"getting entry info in zip "
175+
"file" );
176+
}
161177

162178
auto file = directory_ / file_info->filename;
163179
status = mz_zip_reader_entry_save_file(
164180
reader_, file.string().c_str() );
165-
OPENGEODE_EXCEPTION( status == MZ_OK,
166-
"[UnzipFile::extract_all] Error extracting entry file" );
181+
if( status != MZ_OK )
182+
{
183+
std::filesystem::remove_all( directory_ );
184+
throw OpenGeodeException( "[UnzipFile::extract_all] Error "
185+
"extracting entry file" );
186+
}
167187
status = mz_zip_reader_goto_next_entry( reader_ );
168188
}
169189
}

0 commit comments

Comments
 (0)