Skip to content

Commit d03fbae

Browse files
committed
refactoring
1 parent 55eda94 commit d03fbae

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

ibf-unpack/IbfUnpack.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,24 @@ public static void Unpack(string archivePath, bool rename = false)
1313
Directory.CreateDirectory(systemDir);
1414

1515
Stream stream = File.OpenRead(archivePath);
16-
int fileNameLength = stream.ReadByte() * 2 - 2;
17-
while (fileNameLength > 0)
16+
int filenameLength = stream.ReadByte() * 2;
17+
while (filenameLength > 0)
1818
{
19-
string fileName = ReadUtf16Le(stream, fileNameLength);
20-
string filePath = Path.Combine(systemDir, fileName);
21-
File.WriteAllText(filePath, string.Empty);
22-
23-
stream.ReadByte();
24-
stream.ReadByte();
19+
string filename = ReadString(stream, filenameLength).Replace("\0", string.Empty);
20+
string filepath = Path.Combine(systemDir, filename);
21+
File.WriteAllText(filepath, string.Empty);
2522

2623
int chunkSize = 65536;
2724
int fileLength = BitConverter.ToInt32(ReadBytes(stream, 4), 0);
2825
for (int i = chunkSize; i < fileLength; i += chunkSize)
2926
{
30-
WriteChunkFromStream(stream, chunkSize, filePath);
27+
ReadAndWriteBytes(stream, chunkSize, filepath);
3128
}
3229

3330
int remained = fileLength - fileLength / chunkSize * chunkSize;
34-
WriteChunkFromStream(stream, remained, filePath);
31+
ReadAndWriteBytes(stream, remained, filepath);
3532

36-
fileNameLength = stream.ReadByte() * 2 - 2;
33+
filenameLength = stream.ReadByte() * 2;
3734
}
3835
stream.Close();
3936

@@ -42,22 +39,28 @@ public static void Unpack(string archivePath, bool rename = false)
4239

4340
private static byte[] ReadBytes(Stream stream, int length)
4441
{
45-
byte[] bytesArray = new byte[length];
46-
stream.Read(bytesArray, 0, length);
47-
return bytesArray;
42+
byte[] bytes = new byte[length];
43+
stream.Read(bytes, 0, length);
44+
return bytes;
4845
}
4946

50-
private static string ReadUtf16Le(Stream stream, int size)
47+
private static string ReadString(Stream stream, int length)
5148
{
52-
byte[] bytesArray = ReadBytes(stream, size);
53-
return Encoding.Unicode.GetString(bytesArray);
49+
byte[] bytes = ReadBytes(stream, length);
50+
return Encoding.Unicode.GetString(bytes);
51+
}
52+
53+
private static void WriteBytes(byte[] bytes, string path)
54+
{
55+
FileStream stream = new FileStream(path, FileMode.Append);
56+
stream.Write(bytes, 0, bytes.Length);
57+
stream.Close();
5458
}
5559

56-
private static void WriteChunkFromStream(Stream stream, int size, string filePath)
60+
private static void ReadAndWriteBytes(Stream stream, int length, string path)
5761
{
58-
byte[] bytesArray = ReadBytes(stream, size);
59-
string chunk = Encoding.UTF8.GetString(bytesArray);
60-
File.AppendAllText(filePath, chunk);
62+
byte[] bytes = ReadBytes(stream, length);
63+
WriteBytes(bytes, path);
6164
}
6265

6366
}

0 commit comments

Comments
 (0)