Created
April 20, 2021 13:44
-
-
Save QIvan/bf88d152c31a35eccf162845ce05c455 to your computer and use it in GitHub Desktop.
Workaround for Bazel repo rules fail to extract unicode archives due to latin-1 hack
This file contains 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 org.apache.commons.compress.archivers.tar.TarArchiveEntry; | |
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; | |
import java.io.*; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.zip.GZIPInputStream; | |
public class Main { | |
private static final int BUFFER_SIZE = 32 * 1024; | |
public static void main(String[] args) throws Exception { | |
GZIPInputStream inputStream = | |
new GZIPInputStream( | |
new BufferedInputStream( | |
new FileInputStream( | |
"/home/ivan/.cache/bazel/_bazel_ivan/bc15f3e663f55c36b0229b6c70ea3b20/external/go_sdk/temp3282280016805607077/go1.15.5.linux-amd64.tar.gz"), | |
BUFFER_SIZE)); | |
TarArchiveInputStream tarStream = new TarArchiveInputStream(inputStream); | |
TarArchiveEntry entry; | |
Path path = Paths.get("/tmp/test"); | |
while ((entry = tarStream.getNextTarEntry()) != null) { | |
System.out.println(entry.getName()); | |
if (entry.isDirectory()) { | |
Path.of(entry.getName()).toFile().mkdirs(); | |
} else if (!entry.isLink() && !entry.isSymbolicLink()) { | |
Path resolve = path.resolve(entry.getName()); | |
resolve.getParent().toFile().mkdirs(); | |
try (FileOutputStream outputStream = new FileOutputStream(resolve.toFile())) { | |
copy(tarStream, outputStream); | |
} catch (FileNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
} | |
public static long copy(InputStream from, OutputStream to) throws IOException { | |
byte[] buf = new byte[BUFFER_SIZE]; | |
long total = 0; | |
while (true) { | |
int r = from.read(buf); | |
if (r == -1) { | |
break; | |
} | |
to.write(buf, 0, r); | |
total += r; | |
} | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment