-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarTarSource.cs
More file actions
40 lines (31 loc) · 1.07 KB
/
Copy pathTarTarSource.cs
File metadata and controls
40 lines (31 loc) · 1.07 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
using System;
using System.IO;
using System.Text;
namespace TarGZImporter;
public static class TarTarSource
{
public struct TarEntry
{
public string Name;
public byte[] Data;
}
public static TarEntry ReadTarEntry(Stream fs)
{
var entry = new TarEntry();
// Read 200 bytes for the header
var header = new byte[0x200];
var bytesRead = fs.Read(header, 0, 0x200);
// Get the name and size of the file
entry.Name = Encoding.ASCII.GetString(header, 0, 100).Replace('\0', ' ').Trim();
if (string.IsNullOrEmpty(entry.Name))
return entry;
// Skip 24 bytes and read 12 for the size string from the header (octal)
var size = Encoding.ASCII.GetString(header, 124, 12).Replace('\0', ' ').Trim();
var sizeInt = Convert.ToInt32(size, 8);
// Round size up to the nearest 512 bytes
var sizeRounded = (sizeInt + 511) & ~511;
entry.Data = new byte[sizeRounded];
bytesRead = fs.Read(entry.Data, 0, sizeRounded);
return entry;
}
}