Skip to content

Commit

Permalink
feat: GZIPInputStream instead of GzipCompressorInputStream to read tgz
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Dec 5, 2024
1 parent 11cdf55 commit 0b2e177
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
Expand All @@ -32,51 +33,70 @@ public class TarGzDecompressor implements IArchiveExtractor {
@Override
public void extract(File archiveFile) throws IOException {
StaticLoggerDispatcher.info(LOGGER, "decompress()" + archiveFile.getName());
File dir = new File(archiveFile + ".dir");

File archiveFileExtractTargetDirectory = new File(archiveFile + ".dir");

try (GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(
new BufferedInputStream(new FileInputStream(archiveFile), CompressedUtils.READ_BUFFER_SIZE));
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
TarArchiveEntry entry;
try (FileInputStream fis = new FileInputStream(archiveFile)) {
try (BufferedInputStream bis = new BufferedInputStream(fis, CompressedUtils.READ_BUFFER_SIZE)) {
try (GZIPInputStream gzipIn = new GZIPInputStream(bis, CompressedUtils.READ_BUFFER_SIZE)) {
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn, CompressedUtils.READ_BUFFER_SIZE)) {
extract(tarIn, archiveFileExtractTargetDirectory);
}
}
}
}
StaticLoggerDispatcher.info(LOGGER, "Untar completed successfully!");
}


/**
* extract file from the TarArchiveInputStream
* @param tarIn
* @param archiveFileExtractTargetDirectory
* @throws IOException
*/
private void extract(TarArchiveInputStream tarIn, File archiveFileExtractTargetDirectory) throws IOException
{
TarArchiveEntry entry;

while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
/** If the entry is a directory, create the directory. **/
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
/** If the entry is a directory, create the directory. **/

// directories if not empty are automatically read in tar entries list
if (!entry.isDirectory()) {
int count;
byte[] data = new byte[32738];
// directories if not empty are automatically read in tar entries list
if (!entry.isDirectory()) {
int count;
byte[] data = new byte[CompressedUtils.WRITE_BUFFER_SIZE];

// temporary name for the file being uncompress
try (FileOutputStream fos = new FileOutputStream(dir.getAbsolutePath() + File.separator
+ ManipString.redoEntryName(entry.getName()) + ".tmp", false);
BufferedOutputStream dest = new BufferedOutputStream(fos, 32738);
GZIPOutputStream zdest = new GZIPOutputStream(dest);) {
while ((count = tarIn.read(data, 0, 32738)) != -1) {
zdest.write(data, 0, count);
// temporary name for the file being uncompress
try (FileOutputStream fos = new FileOutputStream(archiveFileExtractTargetDirectory.getAbsolutePath() + File.separator
+ ManipString.redoEntryName(entry.getName()) + ".tmp", false))
{
try(BufferedOutputStream dest = new BufferedOutputStream(fos, CompressedUtils.WRITE_BUFFER_SIZE))
{
try(GZIPOutputStream zdest = new GZIPOutputStream(dest, CompressedUtils.WRITE_BUFFER_SIZE))
{
while ((count = tarIn.read(data, 0, CompressedUtils.READ_BUFFER_SIZE)) != -1) {
zdest.write(data, 0, count);
}
}
}
}

// rename the file when over makes it thread safe and available for other
// threads waiting
try {
FileUtilsArc.renameTo(
new File(dir.getAbsolutePath() + File.separator + ManipString.redoEntryName(entry.getName())
+ ".tmp"),
new File(
dir.getAbsolutePath() + File.separator + ManipString.redoEntryName(entry.getName()))
//
);
} catch (ArcException e) {
throw new IOException(e);
}
// rename the file when over makes it thread safe and available for other
// threads waiting
try {
FileUtilsArc.renameTo(
new File(archiveFileExtractTargetDirectory.getAbsolutePath() + File.separator + ManipString.redoEntryName(entry.getName())
+ ".tmp"),
new File(
archiveFileExtractTargetDirectory.getAbsolutePath() + File.separator + ManipString.redoEntryName(entry.getName()))
//
);
} catch (ArcException e) {
throw new IOException(e);
}
}

}

StaticLoggerDispatcher.info(LOGGER, "Untar completed successfully!");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class CompressedUtils {

private static final Logger LOGGER = LogManager.getLogger(CompressedUtils.class);

public static final int READ_BUFFER_SIZE = 131072;

public static final int READ_BUFFER_SIZE = 81920;
public static final int WRITE_BUFFER_SIZE = 81920;

private CompressedUtils() {
throw new IllegalStateException("Utility class");
}
Expand Down

0 comments on commit 0b2e177

Please sign in to comment.