Java文件压缩(apache.tools.zip)
由于jdk自带的压缩功能存在编码问题,所以使用apache工具包。/**
*
* @function:文件zip压缩
* @param zipPath
* 压缩目标路径
* @param srcPath
* 被压缩文件路径
* @throws BusinessException
* @author: mengqingyu 2013-8-22 上午10:59:37
*/
public static void zipFiles(String zipPath, String srcPath) throws BusinessException {
File srcFile = new File(srcPath);
ZipOutputStream zos = null;
InputStream is = null;
byte[] buf = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));
is = new BufferedInputStream(new FileInputStream(srcFile));
buf = new byte;
is.read(buf);
} catch (Exception e) {
log.error(e);
} finally {
try {
is.close();
} catch (IOException e1) {
log.error(e1);
}
}
try {
zos.putNextEntry(new ZipEntry(srcFile.getName()));
zos.write(buf);
zos.setEncoding("gbk");
} catch (IOException e) {
log.error(e);
} finally {
try {
zos.closeEntry();
zos.close();
} catch (IOException e1) {
log.error(e1);
}
}
}
页:
[1]