Trabajar con archivos GZip

**Descripci�n general

La API Aspose.ZIP para Java le permite crear y gestionar archivos GZip en sus aplicaciones sin necesidad de otras aplicaciones de terceros. La API Aspose.ZIP proporciona la clase GZipArchive para trabajar con archivos GZip. Esta clase proporciona varios m�todos para realizar operaciones con archivos comprimidos.

El algoritmo de compresi�n Gzip se basa en el algoritmo DEFLATE, que es una combinaci�n de LZ77 y la codificaci�n Huffman.

Comprimir un archivo

El siguiente ejemplo de c�digo muestra c�mo comprimir un archivo utilizando la instancia GZipArchive.

1try (GzipArchive archive = new GzipArchive()) {
2    archive.setSource(dataDir + "data.bin");
3    archive.save(dataDir + "archive.gz");
4}

Abrir archivo GZIP

El siguiente ejemplo de c�digo muestra c�mo abrir un archivo GZip.

 1try (GzipArchive archive = new GzipArchive(directoriodatos + "archivo.gz")) {
 2    try (FileOutputStream extrado = new FileOutputStream(directoriodatos + "datos.bin")) {
 3        InputStream desempaquetado = archive.open();
 4        byte[] b = nuevo byte[8192];
 5        int bytesRead;
 6        while (0 < (bytesRead = unpacked.read(b, 0, b.length)) {
 7            extrado.escribir(b, 0, bytesRead);
 8        }
 9    }
10} catch (IOException ex) {
11    System.out.println(ex);
12}

Extraer al flujo de salida

El siguiente ejemplo de c�digo muestra c�mo abrir un archivo desde un stream y extraerlo a un ByteArrayOutputStream.

 1ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 2try (GzipArchive archive = new GzipArchive(new FileInputStream(dataDir + "sample.gz"))) {
 3    byte[] b = nuevo byte[8192];
 4    int bytesRead;
 5    InputStream archiveStream = archive.open();
 6    while (0 < (bytesRead = archiveStream.read(b, 0, b.length)) {
 7        outputStream.write(b, 0, bytesRead);
 8    }
 9    System.out.println(archive.getName());
10} catch (IOException ex) {
11    System.out.println(ex);
12}

Guardar en el flujo de salida

El siguiente ejemplo de c�digo muestra c�mo abrir y guardar en OutputStream.

1ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
2try (GzipArchive archive = new GzipArchive()) {
3    archive.setSource(new File(dataDir + "data.bin"));
4    archive.save(outputStream);
5}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.