Created
December 20, 2012 12:59
-
-
Save johnkil/4345164 to your computer and use it in GitHub Desktop.
Implementation of two versions of the utilities to decompress tar.gz archives (apache tar & jtar).
This file contains hidden or 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 java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.zip.GZIPInputStream; | |
import org.xeustechnologies.jtar.TarEntry; | |
import org.xeustechnologies.jtar.TarInputStream; | |
/** | |
* | |
* The utility class is responsible for unpacking tar or tar.gz packages. | |
* The class uses <a href="http://code.google.com/p/jtar/">Java Tar library</a>. | |
* | |
* @author johnkil | |
* | |
*/ | |
public class JTarUtils { | |
private static final String LOG_TAG = JTarUtils.class.getSimpleName(); | |
private static final int BUFFER_SIZE = 8 * 1024; | |
/** | |
* Unpack the archive to specified directory. | |
* | |
* @param file tar or tar.gz filed | |
* @param outputDir destination directory | |
* @param isGZipped true if the file is gzipped. | |
* @return true in case of success, otherwise - false | |
*/ | |
public static boolean untarFile(File file, File outputDir, boolean isGZipped) { | |
Log.d(LOG_TAG, "Unpacking %s to %s", file.getAbsolutePath(), outputDir.getAbsoluteFile()); | |
boolean result = false; | |
FileInputStream fileInputStream = null; | |
TarInputStream tarArchiveInputStream = null; | |
try { | |
fileInputStream = new FileInputStream(file); | |
tarArchiveInputStream = (isGZipped) ? | |
new TarInputStream(new GZIPInputStream(fileInputStream, BUFFER_SIZE)) : | |
new TarInputStream(new BufferedInputStream(fileInputStream, BUFFER_SIZE)); | |
result = untar(tarArchiveInputStream, outputDir); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} finally { | |
if (tarArchiveInputStream != null) { | |
try { | |
tarArchiveInputStream.close(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
} else if (fileInputStream != null) { | |
try { | |
fileInputStream.close(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
} | |
} | |
return result; | |
} | |
/** | |
* Unpack data from the stream to specified directory. | |
* | |
* @param in stream with tar data | |
* @param outputDir destination directory | |
* @return true in case of success, otherwise - false | |
*/ | |
public static boolean untar(TarInputStream in, File outputDir) { | |
boolean result = false; | |
try { | |
TarEntry entry; | |
while ((entry = in.getNextEntry()) != null) { | |
final File file = new File(outputDir, entry.getName()); | |
if (entry.isDirectory()) { | |
if (!file.exists()) { | |
if (file.mkdirs()) { | |
Log.d(LOG_TAG, "%s directory created", file); | |
} else { | |
Log.w(LOG_TAG, "%s failure to create directory.", file); | |
return false; | |
} | |
} else { | |
Log.w(LOG_TAG, "%s directory is already created", file); | |
} | |
} else { | |
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); | |
try { | |
FileUtils.copyFile(in, out); | |
out.flush(); | |
} finally { | |
try { | |
out.close(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
} | |
Log.d(LOG_TAG, "%s file created", file); | |
} | |
} | |
result = true; | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
return result; | |
} | |
} |
This file contains hidden or 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 java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.zip.GZIPInputStream; | |
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; | |
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; | |
/** | |
* | |
* The utility class is responsible for unpacking tar or tar.gz packages. | |
* The class uses <a href="http://commons.apache.org/compress/">Apache Commons Compress</a> library. | |
* | |
* @author johnkil | |
* | |
*/ | |
public class TarUtils { | |
private static final String LOG_TAG = TarUtils.class.getSimpleName(); | |
private static final int BUFFER_SIZE = 8 * 1024; | |
/** | |
* Unpack the archive to specified directory. | |
* | |
* @param file tar or tar.gz filed | |
* @param outputDir destination directory | |
* @param isGZipped true if the file is gzipped. | |
* @return true in case of success, otherwise - false | |
*/ | |
public static boolean untarFile(File file, File outputDir, boolean isGZipped) { | |
Log.d(LOG_TAG, "Unpacking %s to %s", file.getAbsolutePath(), outputDir.getAbsoluteFile()); | |
boolean result = false; | |
FileInputStream fileInputStream = null; | |
TarArchiveInputStream tarArchiveInputStream = null; | |
try { | |
fileInputStream = new FileInputStream(file); | |
tarArchiveInputStream = (isGZipped) ? | |
new TarArchiveInputStream(new GZIPInputStream(fileInputStream), BUFFER_SIZE) : | |
new TarArchiveInputStream(new BufferedInputStream(fileInputStream), BUFFER_SIZE); | |
result = untar(tarArchiveInputStream, outputDir); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} finally { | |
if (tarArchiveInputStream != null) { | |
try { | |
tarArchiveInputStream.close(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
} else if (fileInputStream != null) { | |
try { | |
fileInputStream.close(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
} | |
} | |
return result; | |
} | |
/** | |
* Unpack data from the stream to specified directory. | |
* | |
* @param in stream with tar data | |
* @param outputDir destination directory | |
* @return true in case of success, otherwise - false | |
*/ | |
public static boolean untar(TarArchiveInputStream in, File outputDir) { | |
boolean result = false; | |
try { | |
TarArchiveEntry entry; | |
while ((entry = in.getNextTarEntry()) != null) { | |
final File file = new File(outputDir, entry.getName()); | |
if (entry.isDirectory()) { | |
if (!file.exists()) { | |
if (file.mkdirs()) { | |
Log.d(LOG_TAG, "%s directory created", file); | |
} else { | |
Log.w(LOG_TAG, "%s failure to create directory.", file); | |
return false; | |
} | |
} else { | |
Log.w(LOG_TAG, "%s directory is already created", file); | |
} | |
} else if (entry.isFile()) { | |
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); | |
try { | |
FileUtils.copyFile(in, out); | |
out.flush(); | |
} finally { | |
try { | |
out.close(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
} | |
Log.d(LOG_TAG, "%s file created", file); | |
} | |
} | |
result = true; | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment