Java using gzip to implement file decompression sample _java

Source: Internet
Author: User

Copy Code code as follows:

Package com.cjonline.foundation.cpe.action;

Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.zip.GZIPInputStream;
Import Java.util.zip.GZIPOutputStream;

Public abstract class Gziputils {

public static final int BUFFER = 1024;
public static final String EXT = ". Gz";

/**
* Data compression
*
* @param data
* @return
* @throws Exception
*/
public static byte[] Compress (byte[] data) throws Exception {
Bytearrayinputstream Bais = new Bytearrayinputstream (data);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();

Compression
Compress (Bais, BAOs);

byte[] output = Baos.tobytearray ();

Baos.flush ();
Baos.close ();

Bais.close ();

return output;
}

/**
* File compression
*
* @param file
* @throws Exception
*/
public static void Compress (file file) throws Exception {
Compress (file, true);
}

/**
* File compression
*
* @param file
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Compress (file file, Boolean delete) throws Exception {
FileInputStream fis = new FileInputStream (file);
FileOutputStream fos = new FileOutputStream (File.getpath () + EXT);

Compress (FIS, FOS);

Fis.close ();
Fos.flush ();
Fos.close ();

if (delete) {
File.delete ();
}
}

/**
* Data compression
*
* @param is
* @param os
* @throws Exception
*/
public static void Compress (InputStream is, OutputStream OS)
Throws Exception {

Gzipoutputstream Gos = new Gzipoutputstream (OS);

int count;
byte data[] = new Byte[buffer];
while (count = is.read (data, 0, BUFFER)!=-1) {
Gos.write (data, 0, count);
}

Gos.finish ();

Gos.flush ();
Gos.close ();
}

/**
* File compression
*
* @param path
* @throws Exception
*/
public static void Compress (String path) throws Exception {
Compress (path, true);
}

/**
* File compression
*
* @param path
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Compress (String path, Boolean delete) throws Exception {
File File = new file (path);
Compress (file, delete);
}

/**
* Data Decompression
*
* @param data
* @return
* @throws Exception
*/
public static byte[] Decompress (byte[] data) throws Exception {
Bytearrayinputstream Bais = new Bytearrayinputstream (data);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();

Decompression

Decompress (Bais, BAOs);

data = Baos.tobytearray ();

Baos.flush ();
Baos.close ();

Bais.close ();

return data;
}

/**
* File Uncompressed
*
* @param file
* @throws Exception
*/
public static void decompress (file file) throws Exception {
Decompress (file, true);
}

/**
* File Uncompressed
*
* @param file
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void decompress (file file, Boolean delete) throws Exception {
FileInputStream fis = new FileInputStream (file);
FileOutputStream fos = new FileOutputStream (File.getpath (). replace (EXT,
""));
Decompress (FIS, FOS);
Fis.close ();
Fos.flush ();
Fos.close ();

if (delete) {
File.delete ();
}
}

/**
* Data Decompression
*
* @param is
* @param os
* @throws Exception
*/
public static void Decompress (InputStream is, OutputStream OS)
Throws Exception {

Gzipinputstream GIS = new Gzipinputstream (IS);

int count;
byte data[] = new Byte[buffer];
while (count = gis.read (data, 0, BUFFER)!=-1) {
Os.write (data, 0, count);
}

Gis.close ();
}

/**
* File Uncompressed
*
* @param path
* @throws Exception
*/
public static void Decompress (String path) throws Exception {
Decompress (path, true);
}

/**
* File Uncompressed
*
* @param path
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Decompress (String path, Boolean delete) throws Exception {
File File = new file (path);
Decompress (file, delete);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.