Skip to content

Commit a06825b

Browse files
Explicitly handle password-protected archives rather than repeatedly throwing exceptions
1 parent 1096232 commit a06825b

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

SabreTools.Serialization/Wrappers/PKZIP.Extraction.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
4343
zipFile = ZipArchive.Open(parts, readerOptions);
4444
}
4545

46+
bool encrypted = false;
4647
foreach (var entry in zipFile.Entries)
4748
{
4849
try
@@ -58,6 +59,17 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
5859
// If the entry is partial due to an incomplete multi-part archive, skip it
5960
if (!entry.IsComplete)
6061
continue;
62+
63+
// If the entry is password-protected, skip it
64+
if (entry.IsEncrypted)
65+
{
66+
if (!encrypted)
67+
{
68+
if (includeDebug) Console.WriteLine("Some or all files in zip are password-protected!");
69+
encrypted = true;
70+
}
71+
continue;
72+
}
6173

6274
// Ensure directory separators are consistent
6375
string filename = entry.Key;

SabreTools.Serialization/Wrappers/RAR.Extraction.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,17 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
7575

7676
if (firstFile)
7777
firstFile = false;
78-
else
78+
else if (entry.IsSolid)
7979
{
80-
isSolid = entry.IsSolid;
80+
// If the RAR is solid and the first entry is password-protected, you won't be able to
81+
// extract the rest of the entries anyway, so just return early.
82+
if (entry.IsEncrypted)
83+
{
84+
if (includeDebug) Console.WriteLine("RAR is password-protected!");
85+
return false;
86+
}
87+
88+
isSolid = true;
8189
break;
8290
}
8391
}
@@ -200,6 +208,7 @@ public static List<string> FindParts(string firstPart)
200208
/// </summary>
201209
private static bool ExtractNonSolid(RarArchive rarFile, string outDir, bool includeDebug)
202210
{
211+
bool encrypted = false;
203212
foreach (var entry in rarFile.Entries)
204213
{
205214
try
@@ -215,6 +224,17 @@ private static bool ExtractNonSolid(RarArchive rarFile, string outDir, bool incl
215224
// If we have a partial entry due to an incomplete multi-part archive, skip it
216225
if (!entry.IsComplete)
217226
continue;
227+
228+
// If the entry is password-protected, skip it
229+
if (entry.IsEncrypted)
230+
{
231+
if (!encrypted)
232+
{
233+
if (includeDebug) Console.WriteLine("Some or all files in RAR are password-protected!");
234+
encrypted = true;
235+
}
236+
continue;
237+
}
218238

219239
// Ensure directory separators are consistent
220240
string filename = entry.Key;

SabreTools.Serialization/Wrappers/SevenZip.Extraction.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,17 @@ public bool Extract(string outputDirectory, bool lookForHeader, bool includeDebu
7474

7575
if (firstFile)
7676
firstFile = false;
77-
else
77+
else if (entry.IsSolid)
7878
{
79-
isSolid = entry.IsSolid;
79+
// If the 7z is solid and the first entry is password-protected, you won't be able to
80+
// extract the rest of the entries anyway, so just return early.
81+
if (entry.IsEncrypted)
82+
{
83+
if (includeDebug) Console.WriteLine("7z is password-protected!");
84+
return false;
85+
}
86+
87+
isSolid = true;
8088
break;
8189
}
8290
}
@@ -168,6 +176,7 @@ public static List<string> FindParts(string firstPart)
168176
/// </summary>
169177
private static bool ExtractNonSolid(SevenZipArchive sevenZip, string outputDirectory, bool includeDebug)
170178
{
179+
bool encrypted = false;
171180
foreach (var entry in sevenZip.Entries)
172181
{
173182
try
@@ -183,6 +192,17 @@ private static bool ExtractNonSolid(SevenZipArchive sevenZip, string outputDirec
183192
// If we have a partial entry due to an incomplete multi-part archive, skip it
184193
if (!entry.IsComplete)
185194
continue;
195+
196+
// If the entry is password-protected, skip it
197+
if (entry.IsEncrypted)
198+
{
199+
if (!encrypted)
200+
{
201+
if (includeDebug) Console.WriteLine("Some or all files in 7z are password-protected!");
202+
encrypted = true;
203+
}
204+
continue;
205+
}
186206

187207
// Ensure directory separators are consistent
188208
string filename = entry.Key;

0 commit comments

Comments
 (0)