Skip to content

Instantly share code, notes, and snippets.

@rbaul
Last active July 28, 2022 11:47
Show Gist options
  • Save rbaul/4f900d3285184f0452e45d3949b00acf to your computer and use it in GitHub Desktop.
Save rbaul/4f900d3285184f0452e45d3949b00acf to your computer and use it in GitHub Desktop.
Read tar tar.gz in memory
/**
* Example of use
*/
public static void main(String[] args) throws IOException {
byte[] tarByte = Files.readAllBytes(Paths.get("some.tar.gz"));
String tar = Base64.getEncoder().encodeToString(tarByte);
}
// Compress
implementation 'org.apache.commons:commons-compress:1.21'
public static void main(String[] args) throws IOException {
String tar = "H4sIANES52EAA+3RTQrCMBBA4RwlJ9CZND/nEW2hUFBqBY9vgxXEhdpFSsX3bWaRgQy85tgd6n5rSpJRSiFPTUGe54NRr0mjauXUiEoKzthQ9KrJ5TzsemtNfWr37/Y+vf+o5t6/abvab4brUOKPHDhGP6O/C+KNlRLHvPr3/mN4LRV+Mr+/Ohfpv4Tcv1ph/0rov4Tc362wv0v0BwAAAAAAAAAAAADgWzfK+kupACgAAA==";
byte[] content = tar.getBytes(StandardCharsets.UTF_8);
TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(content))));
try {
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
while (tarEntry != null) {
byte[] btoRead = new byte[1024];
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int len = 0;
while ((len = tarIn.read(btoRead)) != -1) {
bout.write(btoRead, 0, len);
}
bout.close();
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment