Created
September 23, 2024 07:11
-
-
Save aztack/46effa1e6aab8a1b71c484533ab57a25 to your computer and use it in GitHub Desktop.
Extract tgz in browser
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
import pako from 'pako'; | |
import { TarReader } from '@gera2ld/tarjs'; | |
// Function to extract files from a .tgz file (Uint8Array) | |
async function extractTgz(tgzData) { | |
try { | |
// Step 1: Decompress the gzip using pako | |
const decompressedData = pako.ungzip(tgzData); | |
// Step 2: Create a Blob from the decompressed data | |
const tarBlob = new Blob([decompressedData]); | |
// Step 3: Use TarReader to extract files from the tar archive | |
const tarReader = await TarReader.load(tarBlob); | |
const extractedFiles = []; | |
const { fileInfos } = tarReader; | |
for (const info of fileInfos) { | |
// Check if the entry is a file (not a directory) | |
if (!info.isDirectory) { | |
// For text files, read content as text | |
const content = await tarReader.getTextFile(info.name); | |
extractedFiles.push({ | |
name: info.name, | |
content: content, | |
}); | |
} | |
} | |
// Return or process the extracted files | |
return extractedFiles; | |
} catch (error) { | |
console.error('Error extracting .tgz:', error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment