Created
July 13, 2021 02:50
-
-
Save x1unix/1b4b526cd87cfab4bb5e0096c3c4ba29 to your computer and use it in GitHub Desktop.
[C#] SharpCompress - get XZ uncompressed size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyProgram { | |
public class XzFileInfo | |
{ | |
private const int XzHeaderSize = 12; | |
public static ulong GetUncompressedSize(string filePath) | |
{ | |
using var file = File.Open(filePath, FileMode.Open); | |
// Read the footer from the end. Footer size is 12 bytes according to the spec. | |
file.Seek(-XzHeaderSize, SeekOrigin.End); | |
var footer = XZFooter.FromStream(file); | |
Debug.WriteLine($"BackwardSize: {footer.BackwardSize}"); | |
// Get xz index offset from BackwardSize and seek to it. | |
file.Seek(-(XzHeaderSize + footer.BackwardSize), SeekOrigin.End); | |
var index = XZIndex.FromStream(file, false); | |
Debug.WriteLine($"Index: number of records - {index.NumberOfRecords}"); | |
// Calculate total uncompressed size of each block. | |
var size = index.Records.Select(r => r.UncompressedSize).Aggregate((acc, x) => acc + x); | |
Debug.WriteLine($"Total size of uncompressed archive: {UnitFormatter.FormatByteSize(size)} ({size} bytes)"); | |
return size; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment