Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream GZIPOutputStream.

Prototype

public GZIPOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

From source file:mase.me.MEFinalRepertoireStat.java

@Override
public void finalStatistics(EvolutionState state, int result) {
    super.finalStatistics(state, result);
    if (compress) {
        try {//  w  ww.  j  a  v a  2  s.  c  o m
            taos = new TarArchiveOutputStream(
                    new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(archiveFile))));
        } catch (IOException ex) {
            Logger.getLogger(BestSolutionGenStat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (!compress && !archiveFile.exists()) {
        archiveFile.mkdirs();
    }

    MESubpopulation sub = (MESubpopulation) state.population.subpops[0];
    Collection<Entry<Integer, Individual>> entries = sub.map.entries();
    for (Entry<Integer, Individual> e : entries) {
        PersistentSolution p = SolutionPersistence.createPersistentController(state, e.getValue(), 0,
                e.getKey());
        p.setUserData(sub.getBehaviourVector(state, e.getValue()));
        try {
            if (compress) {
                SolutionPersistence.writeSolutionToTar(p, taos);
            } else {
                SolutionPersistence.writeSolutionInFolder(p, archiveFile);
            }
        } catch (IOException ex) {
            Logger.getLogger(BestSolutionGenStat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (compress) {
        try {
            taos.close();
        } catch (IOException ex) {
            Logger.getLogger(MEFinalRepertoireStat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.ltasks.GZipPostMethod.java

@Override
protected RequestEntity generateRequestEntity() {
    if (mIsGzip) {
        try {// w ww  .  j ava2 s. co  m
            String contentStr = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());

            ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
            originalContent.write(EncodingUtil.getAsciiBytes(contentStr));

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos);
            GZIPOutputStream gzipOut = new GZIPOutputStream(baos);

            originalContent.writeTo(gzipOut);

            gzipOut.finish();
            byte[] content = baos.toByteArray();

            ByteArrayRequestEntity entity = new ByteArrayRequestEntity(content, FORM_URL_ENCODED_CONTENT_TYPE);
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    } else {
        return super.generateRequestEntity();
    }
}

From source file:com.breadwallet.tools.util.BRCompressor.java

public static byte[] gZipCompress(byte[] data) {
    if (data == null)
        return null;
    byte[] compressedData = null;
    try {/*from  ww w.  j a  va2s. co  m*/
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length);
        try {
            GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
            try {
                zipStream.write(data);
            } finally {
                try {
                    zipStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            try {
                byteStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compressedData = byteStream.toByteArray();
    } catch (Exception e) {
        FirebaseCrash.report(e);
        e.printStackTrace();
    }
    return compressedData;
}

From source file:com.thoughtworks.go.websocket.MessageEncoding.java

public static byte[] encodeMessage(Message msg) {
    String encode = gson.toJson(msg);
    try {/*from   w  w w . j a  va 2 s. co  m*/
        try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
            try (GZIPOutputStream out = new GZIPOutputStream(bytes)) {
                out.write(encode.getBytes(StandardCharsets.UTF_8));
                out.finish();
            }
            return bytes.toByteArray();
        }
    } catch (IOException e) {
        throw bomb(e);
    }
}

From source file:ch.ledcom.jpreseed.cli.JPreseed.java

public final void create(JPreseedArguments arguments) throws IOException {
    try (InputImage image = getSourceImage(arguments);
            GZIPOutputStream newImage = new GZIPOutputStream(
                    Files.newOutputStream(arguments.getTargetImage()))) {
        ByteBuffer sysConfigCfg = ByteBuffer.wrap(Files.readAllBytes(arguments.getSysConfigFile()));
        usbCreator.create(image.getContent(), newImage, sysConfigCfg, arguments.getPreseeds());
    }/* www.j a  v a  2s  .  c  om*/
}

From source file:ch.ledcom.jpreseed.UsbCreatorTest.java

@Test
public void createUsb() throws IOException {
    Date startTime = new Date();
    ByteBuffer sysLinuxCfg = ByteBuffer.wrap("sysLinuxCfg".getBytes());
    Set<Path> preseeds = Collections.singleton(HELLO_WORLD_TXT);

    try (InputStream srcBootImg = Files.newInputStream(VFAT_IMG_GZ);
            GZIPOutputStream newBootImg = new GZIPOutputStream(Files.newOutputStream(NEW_IMAGE))) {
        new UsbCreator().create(srcBootImg, newBootImg, sysLinuxCfg, preseeds);
    }//from   w w w . j  a va 2  s . c  o  m

    assertThat(NEW_IMAGE).exists();

    FileSystem fileSystem = FileSystemFactory.create(RamDisk.readGzipped(NEW_IMAGE.toFile()), true);

    FsDirectoryEntry newInitRdGzEntry = fileSystem.getRoot().getEntry(INITRD_GZ);
    assertThat(newInitRdGzEntry).hasBeenModifiedAt(startTime, 2000);
    assertThat(fileSystem.getRoot().getEntry(SYSLINUX_CFG)).hasBeenModifiedAt(startTime, 2000);

    CpioArchiveInputStream initRdCpio = getCpioArchiveInputStream(newInitRdGzEntry);
    assertThat(initRdCpio).hasSingleEntry(HELLO_WORLD_TXT.getFileName().toString());
}

From source file:io.druid.data.input.impl.prefetch.RetryingInputStreamTest.java

@Before
public void setup() throws IOException {
    testFile = temporaryFolder.newFile();

    try (FileOutputStream fis = new FileOutputStream(testFile);
            GZIPOutputStream gis = new GZIPOutputStream(fis);
            DataOutputStream dis = new DataOutputStream(gis)) {
        for (int i = 0; i < 10000; i++) {
            dis.writeInt(i);//www.j  ava 2 s .  c  o  m
        }
    }

    throwError = false;

    final InputStream retryingInputStream = new RetryingInputStream<>(testFile, new ObjectOpenFunction<File>() {
        @Override
        public InputStream open(File object) throws IOException {
            return new TestInputStream(new FileInputStream(object));
        }

        @Override
        public InputStream open(File object, long start) throws IOException {
            final FileInputStream fis = new FileInputStream(object);
            Preconditions.checkState(fis.skip(start) == start);
            return new TestInputStream(fis);
        }
    }, e -> e instanceof IOException, MAX_RETRY);

    inputStream = new DataInputStream(new GZIPInputStream(retryingInputStream));

    throwError = true;
}

From source file:mase.stat.CompetitiveBestStat.java

@Override
public void setup(EvolutionState state, Parameter base) {
    super.setup(state, base);
    compress = state.parameters.getBoolean(base.push(P_COMPRESS), null, true);
    int n = state.parameters.getInt(new Parameter("pop.subpops"), null);
    outFile = new File[n];
    for (int i = 0; i < n; i++) {
        outFile[i] = state.parameters.getFile(base.push(P_FILE), null);
        String newName = compress ? outFile[i].getName().replace(".tar.gz", "." + i + ".tar.gz")
                : outFile[i].getName() + "." + i;
        outFile[i] = new File(outFile[i].getParent(), jobPrefix + newName);
    }/*from   ww w.jav  a 2  s . c om*/
    if (compress) {
        taos = new TarArchiveOutputStream[n];
        for (int i = 0; i < n; i++) {
            try {
                taos[i] = new TarArchiveOutputStream(
                        new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(outFile[i]))));
            } catch (IOException ex) {
                Logger.getLogger(BestSolutionGenStat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (!compress && !outFile[i].exists()) {
            outFile[i].mkdirs();
        }
    }
    if (state.parameters.getBoolean(base.push(P_KEEP_LAST), null, true)) {
        last = new File[n];
        for (int i = 0; i < n; i++) {
            last[i] = new File(outFile[i].getParent(), jobPrefix + "last." + i + ".xml");
        }
    }
}

From source file:org.shredzone.cilla.plugin.sitemap.SitemapView.java

/**
 * Renders a sitemap of all pages./*from   w  ww .j a  va2s . c  om*/
 */
@View(pattern = "/sitemap.xml.gz", name = "sitemap")
public void sitemapView(HttpServletRequest req, HttpServletResponse resp) throws ViewException {
    try {
        resp.setContentType("text/xml");
        resp.setHeader("Content-Encoding", "gzip");

        try (GZIPOutputStream go = new GZIPOutputStream(resp.getOutputStream())) {
            SitemapWriter writer = new SitemapWriter(go);

            writer.writeHeader();
            writeHome(writer);
            writePages(writer);
            writer.writeFooter();
            writer.flush();

            go.finish();
        }
    } catch (IOException ex) {
        throw new ViewException(ex);
    }
}

From source file:jetbrains.exodus.util.CompressBackupUtil.java

@NotNull
public static File backup(@NotNull final Backupable target, @NotNull final File backupRoot,
        @Nullable final String backupNamePrefix, final boolean zip) throws Exception {
    if (!backupRoot.exists() && !backupRoot.mkdirs()) {
        throw new IOException("Failed to create " + backupRoot.getAbsolutePath());
    }//from   www . j ava  2  s  .c om
    final File backupFile;
    final BackupStrategy strategy = target.getBackupStrategy();
    strategy.beforeBackup();
    try {
        final ArchiveOutputStream archive;
        if (zip) {
            final String fileName = getTimeStampedZipFileName();
            backupFile = new File(backupRoot,
                    backupNamePrefix == null ? fileName : backupNamePrefix + fileName);
            final ZipArchiveOutputStream zipArchive = new ZipArchiveOutputStream(
                    new BufferedOutputStream(new FileOutputStream(backupFile)));
            zipArchive.setLevel(Deflater.BEST_COMPRESSION);
            archive = zipArchive;
        } else {
            final String fileName = getTimeStampedTarGzFileName();
            backupFile = new File(backupRoot,
                    backupNamePrefix == null ? fileName : backupNamePrefix + fileName);
            archive = new TarArchiveOutputStream(
                    new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(backupFile))));
        }
        try (ArchiveOutputStream aos = archive) {
            for (final BackupStrategy.FileDescriptor fd : strategy.listFiles()) {
                if (strategy.isInterrupted()) {
                    break;
                }
                final File file = fd.getFile();
                if (file.isFile()) {
                    final long fileSize = Math.min(fd.getFileSize(), strategy.acceptFile(file));
                    if (fileSize > 0L) {
                        archiveFile(aos, fd.getPath(), file, fileSize);
                    }
                }
            }
        }
        if (strategy.isInterrupted()) {
            logger.info("Backup interrupted, deleting \"" + backupFile.getName() + "\"...");
            IOUtil.deleteFile(backupFile);
        } else {
            logger.info("Backup file \"" + backupFile.getName() + "\" created.");
        }
    } catch (Throwable t) {
        strategy.onError(t);
        throw ExodusException.toExodusException(t, "Backup failed");
    } finally {
        strategy.afterBackup();
    }
    return backupFile;
}