Skip to content

Commit

Permalink
fix: read seek bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Oct 2, 2023
1 parent 5a81f05 commit 5037d37
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/DotFastLZ.Package/SixPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ public static FileStream OpenFile(string filePath, FileMode mode, FileAccess acc
{
return new FileStream(filePath, mode, access, share);
}
catch (Exception)
catch (Exception /* e */)
{
//Console.WriteLine(e.Message);
return null;
}
}
Expand Down Expand Up @@ -392,7 +393,7 @@ public static int unpack_file(string input_file)
Console.Write($"Archive: {input_file}");

/* position of first chunk */
ifs.Seek(8, SeekOrigin.Current);
ifs.Seek(8, SeekOrigin.Begin);

/* initialize */
string output_file = string.Empty;
Expand Down Expand Up @@ -453,7 +454,8 @@ out var chunk_extra

/* get file to extract */
int name_length = FastLZ.ReadUInt16(buffer, 8);
output_file = Encoding.UTF8.GetString(buffer, 10, name_length);
output_file = Encoding.UTF8.GetString(buffer, 10, name_length - 1);
output_file = output_file.Trim();

/* check if already exists */
f = OpenFile(output_file, FileMode.Open);
Expand All @@ -466,10 +468,10 @@ out var chunk_extra
else
{
/* create the file */
f = OpenFile(output_file, FileMode.Create, FileAccess.Write);
f = OpenFile(output_file, FileMode.CreateNew, FileAccess.Write, FileShare.Write);
if (null == f)
{
Console.WriteLine($"Can't create file {output_file}. Skipped.");
Console.WriteLine($"Can't create file {output_file} Skipped.");
}
else
{
Expand Down Expand Up @@ -584,13 +586,18 @@ out var chunk_extra
}

/* for progress, if everything is fine */
if (null != f)
//if (null != f)
{
int last_percent = (int)percent;
if (decompressed_size < (1 << 24))
{
percent = total_extracted * 100 / decompressed_size;
}
else
{
percent = total_extracted / 256 * 100 / (decompressed_size >> 8);
}

percent >>= 1;
while (last_percent < (int)percent)
{
Expand All @@ -601,7 +608,7 @@ out var chunk_extra
}

/* position of next chunk */
ifs.Seek(pos + 16 + chunk_size, SeekOrigin.Current);
ifs.Seek(pos + 16 + chunk_size, SeekOrigin.Begin);
}

Console.WriteLine("");
Expand Down

0 comments on commit 5037d37

Please sign in to comment.