我试了一次,没发现大小不一致啊,代码如下:import java.util.*;
import java.util.zip.*;
import java.io.*;class Test{ public static void main(String[] args) throws Exception {
byte[] buffer = "abcdefghigklmnopqrstuvwxyz0123456789".getBytes();
System.out.println("buffer's size:"+buffer.length);
ByteArrayOutputStream o = new ByteArrayOutputStream();
//建立gzip压缩输出流
GZIPOutputStream gzout = new GZIPOutputStream(o);
gzout.write(buffer);
gzout.finish();
gzout.close();
//返回压缩字节流
byte[] data_ = o.toByteArray();
o.close();
//解压的代码:
byte[] buf = new byte[4096*2];
//建立字节数组输入流
ByteArrayInputStream i = new ByteArrayInputStream(data_);
//建立gzip解压输入流
GZIPInputStream gzin = new GZIPInputStream(i);
int size = gzin.read(buf);
i.close();
gzin.close();
byte b[] = new byte[size];
System.arraycopy(buf,0,b,0,size);
System.out.println("buf's size:"+size);
}}
import java.util.zip.*;
import java.io.*;class Test{ public static void main(String[] args) throws Exception {
byte[] buffer = "abcdefghigklmnopqrstuvwxyz0123456789".getBytes();
System.out.println("buffer's size:"+buffer.length);
ByteArrayOutputStream o = new ByteArrayOutputStream();
//建立gzip压缩输出流
GZIPOutputStream gzout = new GZIPOutputStream(o);
gzout.write(buffer);
gzout.finish();
gzout.close();
//返回压缩字节流
byte[] data_ = o.toByteArray();
o.close();
//解压的代码:
byte[] buf = new byte[4096*2];
//建立字节数组输入流
ByteArrayInputStream i = new ByteArrayInputStream(data_);
//建立gzip解压输入流
GZIPInputStream gzin = new GZIPInputStream(i);
int size = gzin.read(buf);
i.close();
gzin.close();
byte b[] = new byte[size];
System.arraycopy(buf,0,b,0,size);
System.out.println("buf's size:"+size);
}}
解决方案 »
- JAVA截取Byte数组中某段数据(数据包括数字、字母、汉字、符号)
- AJax用户名已存在
- jar是什么文件?
- 【求助】谁知道基于SWT的java报表制作工具
- java连接通过jdbc连接mssql怎么配置啊
- 自己定义的interface应该在哪调用,如何调用?
- 又是 连接数据库——ClassNotFoundException!
- 散分咯!!!!答对一个给20分!!!!(多选题)
- 一个数据库操作等待比较长的时间,想弄个等待窗口,不知怎么用多线程实现,麻烦给个例子
- 我在sun的网站上下了jdk1.4,请问怎么把JBuilder6的jdk升级到jdk1.4?
- JDK问题:编译.java文件时为何总报错,不能读文件?
- 急!!请各位帮我看看这段代码,为什么编译不通过,关于ActionListener使用的问题(初学者)
*
* 描述:压缩指定文件名的文件为zip格式
* @param fileName
*/
public static void jZip(String[] rgstring, String fileName) { ZipOutputStream zipoutputstream = null; try {
zipoutputstream =
new ZipOutputStream(new FileOutputStream(fileName)); zipoutputstream.setMethod(ZipOutputStream.DEFLATED); for (int i = 0; i < rgstring.length; i++) { File file = new File(rgstring[i]); byte[] rgb = new byte[1000]; int n = 0; FileInputStream fileinputstream = null; //Calculate the CRC-32 value. This isn't strictly necessary
//for deflated entries, but it doesn't hurt. CRC32 crc32 = new CRC32(); fileinputstream = new FileInputStream(file); while ((n = fileinputstream.read(rgb)) > -1) {
crc32.update(rgb, 0, n);
} fileinputstream.close(); //Create a zip entry. ZipEntry zipentry = new ZipEntry(file.getName()); logger.info(">>>>fileName<<<<" + file.getName()); zipentry.setSize(file.length());
zipentry.setTime(file.lastModified());
zipentry.setCrc(crc32.getValue()); //Add the zip entry and associated data. zipoutputstream.putNextEntry(zipentry); fileinputstream = new FileInputStream(file); while ((n = fileinputstream.read(rgb)) > -1) {
zipoutputstream.write(rgb, 0, n);
} fileinputstream.close(); } zipoutputstream.closeEntry(); zipoutputstream.close(); } catch (Exception e) {
logger.info(e);
}
}
*
* 描述:解压缩zip文件
* @param file
*/
public static void jUnZip(String file) { try { ZipInputStream zipinputstream =
new ZipInputStream(new FileInputStream(file)); while (true) {
// Get the next zip entry. Break out of the loop if there are
// no more. ZipEntry zipentry = zipinputstream.getNextEntry(); if (zipentry == null)
break; // Read data from the zip entry. The read() method will return
// -1 when there is no more data to read. byte[] rgb = new byte[1000]; int n; while ((n = zipinputstream.read(rgb)) > -1) {
// In real life, you'd probably write the data to a file.
} zipinputstream.closeEntry();
} zipinputstream.close();
} catch (Exception e) {
logger.info(e);
}
}