Skip to content

Instantly share code, notes, and snippets.

@korniltsev
Forked from johnkil/GZIPUtils.java
Created October 23, 2012 13:49
Show Gist options
  • Save korniltsev/3938847 to your computer and use it in GitHub Desktop.
Save korniltsev/3938847 to your computer and use it in GitHub Desktop.
Utilities for working with GZIP archives.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import travel.opas.core.util.Log;
/**
* Utilities for working with GZIP archives.
*
* @author e.shishkin
*
*/
public class GZIPUtils {
private static final String LOG_TAG = GZIPUtils.class.getSimpleName();
/**
* Invisible constructor.
*/
private GZIPUtils() {}
/**
* Untar an input file into an output file.
* <p>
* The output file is created in the output folder, having the same name as
* the input file, minus the '.tar' extension.
*
* @param inputFile the input .tar file
* @param outputDir the output directory file.
* @return The {@link List} of {@link File}s with the untared content.
* @throws IOException
* @throws FileNotFoundException
* @throws ArchiveException
*/
public static List<File> unTar(final File inputFile, final File outputDir)
throws FileNotFoundException, IOException, ArchiveException {
Log.v(LOG_TAG, "unTar() called: inputFile=[%s], outputDir=[%s]",
inputFile.getAbsoluteFile(), outputDir.getAbsoluteFile());
final List<File> untaredFiles = new LinkedList<File>();
final TarArchiveInputStream in =
(TarArchiveInputStream) new ArchiveStreamFactory()
.createArchiveInputStream("tar", new FileInputStream(inputFile));
try {
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
Log.d(LOG_TAG, "Attempting to write output directory %s.",
outputFile.getAbsolutePath());
if (!outputFile.exists()) {
Log.d(LOG_TAG, "Attempting to create output directory %s.",
outputFile.getAbsolutePath());
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format(
"Couldn't create directory %s.",
outputFile.getAbsolutePath()));
}
}
} else {
Log.d(LOG_TAG, "Creating output file %s.", outputFile.getAbsolutePath());
final OutputStream out = new FileOutputStream(outputFile);
try {
IOUtils.copy(in, out);
} finally {
out.close();
}
}
untaredFiles.add(outputFile);
}
} finally {
in.close();
}
return untaredFiles;
}
/**
* Ungzip an input file into an output file.
* <p>
* The output file is created in the output folder, having the same name as
* the input file, minus the '.gz' extension.
*
* @param inputFile the input .gz file
* @param outputDir the output directory file.
* @return The {@File} with the ungzipped content.
* @throws IOException
* @throws FileNotFoundException
*/
public static File unGzip(final File inputFile, final File outputDir)
throws FileNotFoundException, IOException {
Log.v(LOG_TAG, "unGzip() called: inputFile=[%s], outputDir=[%s]",
inputFile.getAbsoluteFile(), outputDir.getAbsoluteFile());
final File outputFile = new File(outputDir, inputFile.getName()
.substring(0, inputFile.getName().length() - 3));
final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
final FileOutputStream out = new FileOutputStream(outputFile);
// this is storage overwritten on each iteration with bytes
int bufferSize = 1024 * 4; // 4 kB
byte[] buffer = new byte[bufferSize];
try {
// we need to know how may bytes were read to write them to the output
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} finally {
out.close();
in.close();
}
return outputFile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment