日期:2014-05-20  浏览次数:20723 次

使用gzip压缩解压文件
在一个压缩包里面,里面是几张图片,然后我要解压出来,根据开始字节和字节长度来去里面的对应的图片并显示到页面。根据gzip流来取到要怎么取呢?

------解决方案--------------------
/****
* 压缩文件
* @author kouyi
* @param srcfile 需要压缩的文件列表
* @param zipfile 压缩后的文件
*/
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Compress the files
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();

}
catch (IOException e) {
e.printStackTrace();
}


/****
* 解压缩文件
* @author kouyi
* @param zipfile 需要解压缩的文件
* @param descDir 解压后的文件目录 */
public static void UnZipFiles(java.io.File zipfile, String descDir) {
try {
// Open the ZIP file
ZipFile zf = new ZipFile(zipfile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements(); ) {
// Get the entry name
ZipEntry entry = ( (ZipEntry) entries.nextElement());
String zipEntryName = entry.getName();
InputStream in = zf.getInputStream(entry);
// System.out.println(zipEntryName);
OutputStream out = new FileOutputStream(descDir + zipEntryName);
byte[] buf1 = new byte[1024];
int len;
while ( (len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
// Close the file and stream
in.close();
out.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
 
}

/**
* 根据路径创建一系列的目录

* @param path
*/
private boolean mkDirectory(String path) {
File file = null;
try {
file = new File(path);
if (!file.exists()) {
return file.mkdirs();
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
file = null;
}
return false;
}
------解决方案--------------------
在解压缩的时候写你的判断逻辑。。。
------解决方案--------------------
逻辑是要根据你自己的具体业务去写的。。。这个只有你自己知道的。。。要学会在别人代码的基础上融入自己的东西,不然对你来说一无所获。。。