Example usage for org.apache.commons.compress.compressors.gzip GzipCompressorInputStream read

List of usage examples for org.apache.commons.compress.compressors.gzip GzipCompressorInputStream read

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.gzip GzipCompressorInputStream read.

Prototype

public int read(byte[] b) throws IOException 

Source Link

Usage

From source file:com.bahmanm.karun.Utils.java

/**
 * Extracts a gzip'ed tar archive.//from   w  w w  .j a va 2s .  c  o  m
 * 
 * @param archivePath Path to archive
 * @param destDir Destination directory
 * @throws IOException 
 */
public synchronized static void extractTarGz(String archivePath, File destDir)
        throws IOException, ArchiveException {
    // copy
    File tarGzFile = File.createTempFile("karuntargz", "", destDir);
    copyFile(archivePath, tarGzFile.getAbsolutePath());

    // decompress
    File tarFile = File.createTempFile("karuntar", "", destDir);
    FileInputStream fin = new FileInputStream(tarGzFile);
    BufferedInputStream bin = new BufferedInputStream(fin);
    FileOutputStream fout = new FileOutputStream(tarFile);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin);
    final byte[] buffer = new byte[1024];
    int n = 0;
    while (-1 != (n = gzIn.read(buffer))) {
        fout.write(buffer, 0, n);
    }
    bin.close();
    fin.close();
    gzIn.close();
    fout.close();

    // extract
    final InputStream is = new FileInputStream(tarFile);
    ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) {
        OutputStream out;
        if (entry.isDirectory()) {
            File f = new File(destDir, entry.getName());
            f.mkdirs();
            continue;
        } else
            out = new FileOutputStream(new File(destDir, entry.getName()));
        IOUtils.copy(ain, out);
        out.close();
    }
    ain.close();
    is.close();
}

From source file:com.zenome.bundlebus.Util.java

public static boolean unZip(@NonNull final File aGzFile, @NonNull final File aOutputFile)
        throws FileNotFoundException, IOException {
    Log.d(TAG, "gz filename : " + aGzFile);
    Log.d(TAG, "output file : " + aOutputFile);

    if (!GzipUtils.isCompressedFilename(aGzFile.getAbsolutePath())) {
        Log.d(TAG, "This file is not compressed file : " + aGzFile.getAbsolutePath());
        return false;
    }/*  w w  w.ja  v  a2 s  . com*/

    final FileInputStream fis = new FileInputStream(aGzFile);
    BufferedInputStream in = new BufferedInputStream(fis);
    FileOutputStream out = new FileOutputStream(aOutputFile);

    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    final byte[] buffer = new byte[4096];
    int n = 0;

    while (-1 != (n = gzIn.read(buffer))) {
        out.write(buffer, 0, n);
    }

    out.close();
    gzIn.close();

    return true;
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void write(InputStream tarGzInputStream, Path outTarGz) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream);
    FileOutputStream fios = new FileOutputStream(outTarGz.toFile());
    int buffersize = 1024;
    final byte[] buffer = new byte[buffersize];
    int n = 0;/*from   w ww  . j a  v a2  s  .c  om*/
    while (-1 != (n = gzipStream.read(buffer))) {
        fios.write(buffer, 0, n);
    }
    fios.close();
    gzipStream.close();
}

From source file:msec.org.GzipUtil.java

static public void unzip(String srcFile) throws Exception {
    GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(srcFile));
    int index = srcFile.indexOf(".gz");
    String destFile = "";
    if (index == srcFile.length() - 3) {
        destFile = srcFile.substring(0, index);
    } else {/* www . j a va 2 s.  com*/
        destFile = srcFile + ".decompress";
    }
    FileOutputStream out = new FileOutputStream(destFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:com.renard.ocr.help.OCRLanguageInstallService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (!intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
        return;/*from  w w w  .  ja v  a2s.c  o  m*/
    }
    final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    if (downloadId != 0) {

        DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        ParcelFileDescriptor file;
        try {
            file = dm.openDownloadedFile(downloadId);
            FileInputStream fin = new FileInputStream(file.getFileDescriptor());
            BufferedInputStream in = new BufferedInputStream(fin);
            FileOutputStream out = openFileOutput("tess-lang.tmp", Context.MODE_PRIVATE);
            GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
            final byte[] buffer = new byte[2048 * 2];
            int n = 0;
            while (-1 != (n = gzIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
            out.close();
            gzIn.close();
            FileInputStream fileIn = openFileInput("tess-lang.tmp");
            TarArchiveInputStream tarIn = new TarArchiveInputStream(fileIn);

            TarArchiveEntry entry = tarIn.getNextTarEntry();

            while (entry != null && !(entry.getName().endsWith(".traineddata")
                    && !entry.getName().endsWith("_old.traineddata"))) {
                entry = tarIn.getNextTarEntry();
            }
            if (entry != null) {
                File tessDir = Util.getTrainingDataDir(this);
                final String langName = entry.getName().substring("tesseract-ocr/tessdata/".length());
                File trainedData = new File(tessDir, langName);
                FileOutputStream fout = new FileOutputStream(trainedData);
                int len;
                while ((len = tarIn.read(buffer)) != -1) {
                    fout.write(buffer, 0, len);
                }
                fout.close();
                String lang = langName.substring(0, langName.length() - ".traineddata".length());
                notifyReceivers(lang);
            }
            tarIn.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            String tessDir = Util.getTessDir(this);
            File targetFile = new File(tessDir, OCRLanguageActivity.DOWNLOADED_TRAINING_DATA);
            if (targetFile.exists()) {
                targetFile.delete();
            }
        }

    }
}

From source file:cc.arduino.contributions.GZippedJsonDownloader.java

private void decompress(File gzipTmpFile, File tmpFile) throws IOException {
    OutputStream os = null;//from  w  w w.j  a  v a2  s. c o  m
    GzipCompressorInputStream gzipIs = null;
    try {
        os = new FileOutputStream(tmpFile);
        gzipIs = new GzipCompressorInputStream(new FileInputStream(gzipTmpFile));
        final byte[] buffer = new byte[4096];
        int n;
        while (-1 != (n = gzipIs.read(buffer))) {
            os.write(buffer, 0, n);
        }
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(gzipIs);
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * ungzip. Given a gzip stream, decompress and store in file in temp_dir
 *///from  www .  j  a  v  a  2 s  .  com
protected File ungzip(File gzf) throws FileNotFoundException {
    //File f = null;
    String rename = gzf.getAbsolutePath().replaceFirst("\\.gz", identifier + ".xml");
    File f = new File(rename);
    try {
        FileInputStream fis = new FileInputStream(gzf);
        FileOutputStream fos = new FileOutputStream(rename);
        GzipCompressorInputStream gzin = new GzipCompressorInputStream(fis);
        final byte[] content = new byte[BUFFER];
        int n = 0;
        while (-1 != (n = gzin.read(content))) {
            fos.write(content, 0, n);
        }

        fos.flush();
        fos.close();

        fis.close();
        gzin.close();
    } catch (IOException ioe) {
        jlog.error("Error processing GZip " + gzf + " Excluding! :: " + ioe);
        return null;
    }
    //try again... what could go wrong
    if (checkMinFileSize(f) && retry_counter < MAX_UNGZIP_RETRIES) {
        retry_counter++;
        f.delete();
        f = ungzip(gzf);
    }
    return f;
}

From source file:air.AirRuntimeGenerator.java

protected void processFlashRuntime(File sdkSourceDirectory, File sdkTargetDirectory) throws Exception {
    final File runtimeDirectory = new File(sdkSourceDirectory, "runtimes");
    final File flashPlayerDirectory = new File(runtimeDirectory, "player");

    File[] versions = flashPlayerDirectory.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.isDirectory() && !"win".equalsIgnoreCase(pathname.getName())
                    && !"lnx".equalsIgnoreCase(pathname.getName())
                    && !"mac".equalsIgnoreCase(pathname.getName());
        }/*from ww w. j  a  va 2 s .c  o m*/
    });
    // The flash-player 9 is installed directly in the player directory.
    if (new File(flashPlayerDirectory, "win").exists()) {
        final File[] extendedVersions = new File[versions.length + 1];
        System.arraycopy(versions, 0, extendedVersions, 0, versions.length);
        extendedVersions[versions.length] = flashPlayerDirectory;
        versions = extendedVersions;
    }

    if (versions != null) {
        for (final File versionDir : versions) {
            // If the versionDir is called "player", then this is the home of the flash-player version 9.
            final String playerVersionString = "player".equalsIgnoreCase(versionDir.getName()) ? "9.0"
                    : versionDir.getName();
            final double playerVersion = Double.valueOf(playerVersionString);
            final NumberFormat doubleFormat = NumberFormat.getInstance(Locale.US);
            doubleFormat.setMinimumFractionDigits(1);
            doubleFormat.setMaximumFractionDigits(1);
            final String version = doubleFormat.format(playerVersion);

            final File targetDir = new File(sdkTargetDirectory, "com/adobe/flash/runtime/" + version);

            // Deploy Windows binaries.
            final File windowsDirectory = new File(versionDir, "win");
            if (windowsDirectory.exists()) {
                // Find out if a flash-player binary exists.
                File flashPlayerBinary = null;
                if (new File(windowsDirectory, "FlashPlayerDebugger.exe").exists()) {
                    flashPlayerBinary = new File(windowsDirectory, "FlashPlayerDebugger.exe");
                } else if (new File(windowsDirectory, "FlashPlayer.exe").exists()) {
                    flashPlayerBinary = new File(windowsDirectory, "FlashPlayer.exe");
                }

                // If a binary exists, copy it to the target and create a pom for it.
                if (flashPlayerBinary != null) {
                    if (!targetDir.exists()) {
                        if (!targetDir.mkdirs()) {
                            throw new RuntimeException(
                                    "Could not create directory: " + targetDir.getAbsolutePath());
                        }
                    }
                    final File targetFile = new File(targetDir, "/runtime-" + version + "-win.exe");
                    copyFile(flashPlayerBinary, targetFile);
                }
            }

            // Deploy Mac binaries.
            final File macDirectory = new File(versionDir, "mac");
            if (macDirectory.exists()) {
                // Find out if a flash-player binary exists.
                File flashPlayerBinary = null;
                if (new File(macDirectory, "Flash Player.app.zip").exists()) {
                    flashPlayerBinary = new File(macDirectory, "Flash Player.app.zip");
                } else if (new File(macDirectory, "Flash Player Debugger.app.zip").exists()) {
                    flashPlayerBinary = new File(macDirectory, "Flash Player Debugger.app.zip");
                }

                // If a binary exists, copy it to the target and create a pom for it.
                if (flashPlayerBinary != null) {
                    if (!targetDir.exists()) {
                        if (!targetDir.mkdirs()) {
                            throw new RuntimeException(
                                    "Could not create directory: " + targetDir.getAbsolutePath());
                        }
                    }
                    final File targetFile = new File(targetDir, "/runtime-" + version + "-mac.zip");
                    copyFile(flashPlayerBinary, targetFile);
                }
            }

            // Deploy Linux binaries.
            final File lnxDirectory = new File(versionDir, "lnx");
            if (lnxDirectory.exists()) {
                // Find out if a flash-player binary exists.
                File flashPlayerBinary = null;
                if (new File(lnxDirectory, "flashplayer.tar.gz").exists()) {
                    flashPlayerBinary = new File(lnxDirectory, "flashplayer.tar.gz");
                } else if (new File(lnxDirectory, "flashplayerdebugger.tar.gz").exists()) {
                    flashPlayerBinary = new File(lnxDirectory, "flashplayerdebugger.tar.gz");
                }

                // Decompress the archive.
                // First unzip it.
                final FileInputStream fin = new FileInputStream(flashPlayerBinary);
                final BufferedInputStream in = new BufferedInputStream(fin);
                final File tempTarFile = File.createTempFile("flex-sdk-linux-flashplayer-binary-" + version,
                        ".tar");
                final FileOutputStream out = new FileOutputStream(tempTarFile);
                final GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
                final byte[] buffer = new byte[1024];
                int n;
                while (-1 != (n = gzIn.read(buffer))) {
                    out.write(buffer, 0, n);
                }
                out.close();
                gzIn.close();

                // Then untar it.
                File uncompressedBinary = null;
                final FileInputStream tarFileInputStream = new FileInputStream(tempTarFile);
                final TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
                        tarFileInputStream);
                ArchiveEntry entry;
                while ((entry = tarArchiveInputStream.getNextEntry()) != null) {
                    if ("flashplayer".equals(entry.getName())) {
                        uncompressedBinary = File.createTempFile("flex-sdk-linux-flashplayer-binary-" + version,
                                ".uexe");
                        final FileOutputStream uncompressedBinaryOutputStream = new FileOutputStream(
                                uncompressedBinary);
                        while (-1 != (n = tarArchiveInputStream.read(buffer))) {
                            uncompressedBinaryOutputStream.write(buffer, 0, n);
                        }
                        uncompressedBinaryOutputStream.close();
                    } else if ("flashplayerdebugger".equals(entry.getName())) {
                        uncompressedBinary = File.createTempFile("flex-sdk-linux-flashplayer-binary-" + version,
                                ".uexe");
                        final FileOutputStream uncompressedBinaryOutputStream = new FileOutputStream(
                                uncompressedBinary);
                        while (-1 != (n = tarArchiveInputStream.read(buffer))) {
                            uncompressedBinaryOutputStream.write(buffer, 0, n);
                        }
                        uncompressedBinaryOutputStream.close();
                    }
                }
                tarFileInputStream.close();

                // If a binary exists, copy it to the target and create a pom for it.
                if (uncompressedBinary != null) {
                    if (!targetDir.exists()) {
                        if (!targetDir.mkdirs()) {
                            throw new RuntimeException(
                                    "Could not create directory: " + targetDir.getAbsolutePath());
                        }
                    }
                    final File targetFile = new File(targetDir, "/runtime-" + version + "-linux.uexe");
                    copyFile(uncompressedBinary, targetFile);

                    // Clean up in the temp directory.
                    if (!uncompressedBinary.delete()) {
                        System.out.println("Could not delete: " + uncompressedBinary.getAbsolutePath());
                    }
                }

                // Clean up in the temp directory.
                if (!tempTarFile.delete()) {
                    System.out.println("Could not delete: " + tempTarFile.getAbsolutePath());
                }
            }

            final MavenMetadata playerArtifact = new MavenMetadata();
            playerArtifact.setGroupId("com.adobe.flash");
            playerArtifact.setArtifactId("runtime");
            playerArtifact.setVersion(version);
            playerArtifact.setPackaging("exe");

            writeDocument(createPomDocument(playerArtifact),
                    new File(targetDir, "runtime-" + version + ".pom"));
        }
    }
}

From source file:edu.mda.bioinfo.ids.UncompressFiles.java

protected boolean unGz(String theGzFile, String theUngzFile) throws IOException {
    boolean uncompressed = false;
    // uncompress
    FileInputStream fin = null;//from   w  w  w.  j  a  v  a 2s  . co  m
    BufferedInputStream in = null;
    FileOutputStream out = null;
    GzipCompressorInputStream gzIn = null;
    try {
        System.out.println("Uncompress::unGz starting for the file " + theGzFile);
        fin = new FileInputStream(theGzFile);
        in = new BufferedInputStream(fin);
        File outfile = new File(theUngzFile);
        out = new FileOutputStream(outfile);
        gzIn = new GzipCompressorInputStream(in);
        final byte[] buffer = new byte[1024];
        int n = 0;
        while (-1 != (n = gzIn.read(buffer))) {
            out.write(buffer, 0, n);
        }
        uncompressed = true;
    } catch (java.io.FileNotFoundException exp) {
        System.err.println("Uncompress::uncompress File not found decompressing " + theGzFile);
        throw exp;
    } catch (java.io.IOException exp) {
        System.err.println("Uncompress::uncompress IOException decompressing " + theGzFile);
        throw exp;
    } finally {
        try {
            out.close();
        } catch (Exception ignore) {
            //ignore
        }
        try {
            gzIn.close();
        } catch (Exception ignore) {
            //ignore
        }
    }
    return (uncompressed);
}

From source file:it.drwolf.ridire.session.async.Mapper.java

private File uncompressGzippedArcFile(File f) throws IOException {
    FileInputStream fin = new FileInputStream(f.getAbsolutePath());
    BufferedInputStream in = new BufferedInputStream(fin);
    File uncompressedFile = new File(FilenameUtils.removeExtension(f.getAbsolutePath()));
    FileOutputStream out = new FileOutputStream(uncompressedFile);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in, true);
    final byte[] buffer = new byte[1024];
    int n = 0;//from w  w w . j  ava  2  s  . co m
    while (-1 != (n = gzIn.read(buffer))) {
        out.write(buffer, 0, n);
    }
    out.close();
    gzIn.close();
    return uncompressedFile;
}