Skip to content

Commit 0b54350

Browse files
Explicitly handle password-protected archives rather than repeatedly throwing exceptions (#47)
* Explicitly handle password-protected archives rather than repeatedly throwing exceptions * Fix logic error * Continues * Remove bool
1 parent 59eddb7 commit 0b54350

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

SabreTools.Serialization/Wrappers/PKZIP.Extraction.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
5858
// If the entry is partial due to an incomplete multi-part archive, skip it
5959
if (!entry.IsComplete)
6060
continue;
61+
62+
// If the entry is password-protected, skip it
63+
if (entry.IsEncrypted)
64+
{
65+
if (includeDebug) Console.WriteLine($"File {entry.Key} in zip is password-protected!");
66+
67+
continue;
68+
}
6169

6270
// Ensure directory separators are consistent
6371
string filename = entry.Key;

SabreTools.Serialization/Wrappers/RAR.Extraction.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,25 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
7474
continue;
7575

7676
if (firstFile)
77+
{
7778
firstFile = false;
78-
else
79+
continue;
80+
}
81+
82+
if (entry.IsSolid)
7983
{
80-
isSolid = entry.IsSolid;
81-
break;
84+
// If the RAR is solid and the first entry is password-protected, you won't be able to
85+
// extract the rest of the entries anyway, so just return early.
86+
if (entry.IsEncrypted)
87+
{
88+
if (includeDebug) Console.WriteLine("RAR is password-protected!");
89+
return false;
90+
}
91+
92+
isSolid = true;
8293
}
94+
95+
break;
8396
}
8497
catch (Exception ex)
8598
{
@@ -215,6 +228,14 @@ private static bool ExtractNonSolid(RarArchive rarFile, string outDir, bool incl
215228
// If we have a partial entry due to an incomplete multi-part archive, skip it
216229
if (!entry.IsComplete)
217230
continue;
231+
232+
// If the entry is password-protected, skip it
233+
if (entry.IsEncrypted)
234+
{
235+
if (includeDebug) Console.WriteLine($"File {entry.Key} in RAR is password-protected!");
236+
237+
continue;
238+
}
218239

219240
// Ensure directory separators are consistent
220241
string filename = entry.Key;

SabreTools.Serialization/Wrappers/SevenZip.Extraction.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,25 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
7373
continue;
7474

7575
if (firstFile)
76+
{
7677
firstFile = false;
77-
else
78+
continue;
79+
}
80+
81+
if (entry.IsSolid)
7882
{
79-
isSolid = entry.IsSolid;
80-
break;
83+
// If the 7z is solid and the first entry is password-protected, you won't be able to
84+
// extract the rest of the entries anyway, so just return early.
85+
if (entry.IsEncrypted)
86+
{
87+
if (includeDebug) Console.WriteLine("7z is password-protected!");
88+
return false;
89+
}
90+
91+
isSolid = true;
8192
}
93+
94+
break;
8295
}
8396
catch (Exception ex)
8497
{
@@ -183,6 +196,14 @@ private static bool ExtractNonSolid(SevenZipArchive sevenZip, string outputDirec
183196
// If we have a partial entry due to an incomplete multi-part archive, skip it
184197
if (!entry.IsComplete)
185198
continue;
199+
200+
// If the entry is password-protected, skip it
201+
if (entry.IsEncrypted)
202+
{
203+
if (includeDebug) Console.WriteLine($"File {entry.Key} in 7z is password-protected!");
204+
205+
continue;
206+
}
186207

187208
// Ensure directory separators are consistent
188209
string filename = entry.Key;

0 commit comments

Comments
 (0)