Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extract from one level below the archive #813

Open
Bluscream opened this issue Mar 9, 2024 · 1 comment
Open

How to extract from one level below the archive #813

Bluscream opened this issue Mar 9, 2024 · 1 comment

Comments

@Bluscream
Copy link

Bluscream commented Mar 9, 2024

I have the following code:

public static void Extract(string archivePath, string destPath, string archiveRoot = "/") {
    using (Stream stream = File.OpenRead(archivePath)) {
        var reader = ArchiveFactory.Open(stream);
        var entries = reader.Entries.ToList();
        if (entries.Count < 1) throw new ArchiveException($"Empty archive: {archivePath}");
        var firstEntry = entries.First();
        var isGithubTarball = (entries.Count == 1 && firstEntry.IsDirectory && firstEntry.Key.Contains("-"));
        Console.WriteLine($"Extracting {archivePath} to {destPath} (root: {archiveRoot}) [entries: {entries.Count}, isGithubTarball: {isGithubTarball}]");
        if (isGithubTarball && archiveRoot == "/") {
            Extract(archivePath, destPath, archiveRoot + firstEntry.Key); return;
        }
        foreach (var entry in reader.Entries) {
            if (!entry.IsDirectory && entry.Key != null) { // Add null check here
                string entryPath = entry.Key;
                if (entryPath.StartsWith(archiveRoot)) {
                    entryPath = entryPath.Substring(archiveRoot.Length);
                }
                entry.WriteToDirectory(Path.Combine(destPath, entryPath), new ExtractionOptions() {
                    ExtractFullPath = true,
                    Overwrite = true
                });
            }
        }
    }
}

and the first entry has a CRC but it's key or other attributes are always null:

Related archive: https://github.com/dayz-cc/serverfiles/archive/refs/heads/main.tar.gz
Related phind convo: https://www.phind.com/agent?cache=cltk80vqr001ola08l98kgz9b

@adamhathcock
Copy link
Owner

use entry.OpenEntryStream and you can point that stream where ever you want...this is what WriteToDirectory uses

https://github.com/adamhathcock/sharpcompress/blob/master/src/SharpCompress/Archives/IArchiveEntryExtensions.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants