/** * Return the uncompressed size for a Tar file. * * @param tarFile The Tarred file * @return Size when uncompressed, in bytes * @throws IOException */ private int getTarSizeUncompressed(File tarFile) throws IOException { int size = 0; TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile))); TarEntry entry; while ((entry = tis.getNextEntry()) != null) { if (!entry.isDirectory()) { size += entry.getSize(); } } return size; }
/** * Untar the contents of a tar file into the given directory, ignoring the relative pathname in * the tar file, and delete the tar file. * * <p>Uses jtar: http://code.google.com/p/jtar/ * * @param tarFile The tar file to be untarred * @param destinationDir The directory to untar into * @throws IOException */ private void untar(File tarFile, File destinationDir) throws IOException { Log.d(TAG, "Untarring..."); final int uncompressedSize = getTarSizeUncompressed(tarFile); Integer percentComplete; int percentCompleteLast = 0; int unzippedBytes = 0; final Integer progressMin = 50; final int progressMax = 100 - progressMin; publishProgress("Uncompressing data for " + languageName + "...", progressMin.toString()); // Extract all the files TarInputStream tarInputStream = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile))); TarEntry entry; while ((entry = tarInputStream.getNextEntry()) != null) { int len; final int BUFFER = 8192; byte data[] = new byte[BUFFER]; String pathName = entry.getName(); String fileName = pathName.substring(pathName.lastIndexOf('/'), pathName.length()); OutputStream outputStream = new FileOutputStream(destinationDir + fileName); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); Log.d(TAG, "Writing " + fileName.substring(1, fileName.length()) + "..."); while ((len = tarInputStream.read(data, 0, BUFFER)) != -1) { bufferedOutputStream.write(data, 0, len); unzippedBytes += len; percentComplete = (int) ((unzippedBytes / (float) uncompressedSize) * progressMax) + progressMin; if (percentComplete > percentCompleteLast) { publishProgress( "Uncompressing data for " + languageName + "...", percentComplete.toString()); percentCompleteLast = percentComplete; } } bufferedOutputStream.flush(); bufferedOutputStream.close(); } tarInputStream.close(); if (tarFile.exists()) { tarFile.delete(); } }