-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkHandler.cs
More file actions
62 lines (51 loc) · 2.18 KB
/
Copy pathChunkHandler.cs
File metadata and controls
62 lines (51 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.IO;
namespace FastFilesCompressor
{
public static class ChunkHandler
{
public static void Chunk(FastFileBinaryReader reader, FastFileBinaryWriter writer)
{
int maximumChunkSize = 65536;
for (int currentPosition = 0; currentPosition < reader.BaseStream.Length; currentPosition += maximumChunkSize)
{
int uncompressedChunkSize = (int)reader.BaseStream.Length - currentPosition;
if (uncompressedChunkSize > maximumChunkSize)
{
uncompressedChunkSize = maximumChunkSize;
}
byte[] inputBuffer = reader.ReadBytes((int)maximumChunkSize);
byte[] outputBuffer = new byte[maximumChunkSize * 2];
int compressedChunkSize = LZ4Handler.LZ4_compress(inputBuffer, outputBuffer, inputBuffer.Length);
writer.Write(compressedChunkSize);
writer.Write((int)uncompressedChunkSize);
writer.Write(outputBuffer, 0, compressedChunkSize);
if (compressedChunkSize % 4 != 0)
{
writer.Write(new byte[4 - compressedChunkSize % 4]);
}
}
}
public static void Dechunk(FastFileBinaryReader reader, FastFileBinaryWriter writer)
{
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
Int32 compressedChunkSize = reader.ReadInt32();
Int32 uncompressedChunkSize = reader.ReadInt32();
byte[] input = reader.ReadBytes(compressedChunkSize);
byte[] output = new byte[uncompressedChunkSize];
int result = LZ4Handler.LZ4_decompress_safe(input, output, input.Length, uncompressedChunkSize);
if (result != uncompressedChunkSize)
{
throw new ArgumentException();
}
writer.Write(output);
// padding
if (compressedChunkSize % 4 != 0)
{
reader.ReadBytes(4 - compressedChunkSize % 4);
}
}
}
}
}