Extract tar.gz file to Java memory

I use the Apache Compress library to read the .tar.gz file, something like this:

    final TarArchiveInputStream tarIn = initializeTarArchiveStream(this.archiveFile);
    try {
        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        while (tarEntry != null) {
            byte[] btoRead = new byte[1024];
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); //<- I don't want this!
            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();
    }

Is it possible not to extract this into a separate file and somehow read it in memory? Maybe in a giant string or something else?

+3
source share
3 answers

Is it possible not to extract this into a separate file and somehow read it in memory? Maybe in a giant string or something else?

Yes of course.

Just replace the code in the inner loop that opens the files and writes them with the code that writes to ByteArrayOutputStream... or a series of such threads.

, TAR (, ), / . , , . . ( , /, ... .)

, , ... , .

+3

ByteArrayOutputStream.

. :

BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); //<- I don't want this!

:

ByteArrayOutputStream bout = new ByteArrayOutputStream();

bout bout.toByteArray(), .

+5

btoread String

String s = String.valueof(byteVar);

and goon, adding the byte value to the line to the end of the file.

0
source

All Articles