Java中java.util.zip压缩和解压

1、用Base64编码在序列化对象和String对象之间进行压缩解压转换

  public static <TargetType extends Serializable> TargetType deserializeFromString(String value)
 throws SerializationUtilsErrorException
 {
  if (null == value)
  {
   return null;
  }
  
  byte[] value_bytes_decoded = Base64.decode(value);
  if (null == value_bytes_decoded)
  {
   throw new DeserializationErrorException(null);
  }
  
  ByteArrayInputStream bytes_is = new ByteArrayInputStream(value_bytes_decoded);
  GZIPInputStream   gzip_is = null;
  ObjectInputStream  object_is = null;
  try
  {
   gzip_is = new GZIPInputStream(bytes_is);
   object_is = new ObjectInputStream(gzip_is);
   return (TargetType)object_is.readObject();
  }
  catch (IOException e)
  {
   throw new DeserializationErrorException(e);
  }
  catch (ClassNotFoundException e)
  {
   throw new DeserializationErrorException(e);
  }
 }
 
 public static String serializeToString(Serializable value)
 throws SerializationUtilsErrorException
 {
  if (null == value) throw new IllegalArgumentException("value can't be null.");

  ByteArrayOutputStream byte_os = new ByteArrayOutputStream();
  GZIPOutputStream  gzip_os = null;
  ObjectOutputStream  object_os = null;
  try
  {
   gzip_os = new GZIPOutputStream(byte_os);
   object_os = new ObjectOutputStream(gzip_os);
   object_os.writeObject(value);
   object_os.flush();
   gzip_os.flush();
   gzip_os.finish();
  }
  catch (IOException e)
  {
   throw new SerializationErrorException(value, e);
  }
  
  byte[] value_bytes_decoded = byte_os.toByteArray();
  
  return Base64.encodeToString(value_bytes_decoded, false);
 }

2、用Deflater和Inflater实现解压和压缩字节数组

//压缩

    byte[] input = "some some bytes to compress".getBytes();
    
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    
    // Give the compressor the data to compress
    compressor.setInput(input);
    compressor.finish();
    
    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    
    // Get the compressed data
    byte[] compressedData = bos.toByteArray();
//解压
    // Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    
    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    
    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();
 
3、对序列化对象进行解压缩
 在java下的对象gzip压缩有着广泛的应用价值.以下是一个简单事例程序.
(串行化的数据对象文件:Data.java)
import java.io.*;
import java.util.zip.*;
public class Data implements Serializable//继承串行序列化接口
{
String name="匹配";
int age=123;
float height=1.902f;
}
(对象压缩解压缩类文件:compressObject.java)
import java.util.zip.*;
import java.io.*;
public final class compressObject
{
//将Data类型数据对象序列化对象压缩,返回字节数组,压缩后的对象数组可写入文件保存或用于网络传输
public static byte[] writeCompressObject(Data object_)
{
byte[] data_=null;
try
{
//建立字节数组输出流
ByteArrayOutputStream o = new ByteArrayOutputStream();
//建立gzip压缩输出流
GZIPOutputStream gzout=new GZIPOutputStream(o);
//建立对象序列化输出流
ObjectOutputStream out = new ObjectOutputStream(gzout);
out.writeObject(object_);
out.flush();
out.close();
gzout.close();
//返回压缩字节流
data_=o.toByteArray();
o.close();
}catch(IOException e)
{
System.out.println(e);
}
return(data_);
}
//将压缩字节数组还原为Data类型数据对象
public static Data readCompressObject(byte[] data_)
{
Data object_=null;
try
{
//建立字节数组输入流
ByteArrayInputStream i = new ByteArrayInputStream(data_);
//建立gzip解压输入流
GZIPInputStream gzin=new GZIPInputStream(i);
//建立对象序列化输入流
ObjectInputStream in = new ObjectInputStream(gzin);
//按制定类型还原对象
object_=(Data)in.readObject();
i.close();
gzin.close();
in.close();
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
return(object_);
}
}
(主程序:test.java)
import java.io.*;
import java.util.zip.*;
public class test
{
public static void main(String[] args)
{
Data testData_=new Data();
//未压缩数据对象内容
System.out.println("name="+testData_.name+" age="+testData_.age+" height="+testData_.height);
//压缩
byte[] i_=compressObject.writeCompressObject(testData_);
/*
可执行保存或网络传输,需要时还原或在对端还原
*/
//解压缩
Data o_=compressObject.readCompressObject(i_);
//解压缩后对象内容
System.out.println("name="+o_.name+" age="+o_.age+" height="+o_.height);
}
}

你可能感兴趣的:(java,网络应用,OS)