使用gzip压缩解压文件解决办法使用gzip压缩解压文件解决办法
使用gzip压缩解压文件
在一个压缩包里面,里面是几张图片,然后我要解压出来,根据开始字节和字节长度来去里面的对应的图片并显示到页面。根据gzip流来取到要怎么取呢,
------解决方案--------------------------------------------------------
/****
* 压缩文件
* @author kouyi
* @param srcfile 需要压缩的文件列表
* @param zipfile 压缩后的文件
*/
publi...
使用gzip压缩解压文件解决办法
使用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;
}
------解决方案--------------------------------------------------------
在解压缩的时候写你的判断逻辑。。。
------解决方案--------------------------------------------------------
逻辑是要根据你自己的具体业务去写的。。。这个只有你自己知道的。。。要学会在别人代码的
基础上融入自己的东西,不然对你来说一无所获。。。
本文档为【使用gzip压缩解压文件解决办法】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。