use of org.apache.tools.zip.ZipEntry in project hudson-2.x by hudson.
the class ZipArchiver method visit.
public void visit(File f, String relativePath) throws IOException {
if (f.isDirectory()) {
ZipEntry dirZipEntry = new ZipEntry(relativePath + '/');
// Setting this bit explicitly is needed by some unzipping applications (see HUDSON-3294).
dirZipEntry.setExternalAttributes(BITMASK_IS_DIRECTORY);
zip.putNextEntry(dirZipEntry);
zip.closeEntry();
} else {
zip.putNextEntry(new ZipEntry(relativePath));
FileInputStream in = new FileInputStream(f);
int len;
while ((len = in.read(buf)) > 0) zip.write(buf, 0, len);
in.close();
zip.closeEntry();
}
entriesWritten++;
}
use of org.apache.tools.zip.ZipEntry in project wechat by dllwh.
the class ZipUtil method zip.
/**
* @Title: zip
* @Description: 压缩文件或者文件目录到指定的zip或者rar包
* @param zipFileName
* 压缩产生的zip文件名--带路径,如果为null或空则默认按文件名生产压缩文件名
* @param source
* 源路径,可以是文件,也可以目录
* @param directory
* 目标路径,压缩文件名
*/
public static void zip(String zipFileName, String source, String directory) {
ZipOutputStream zos = null;
BufferedInputStream bis = null;
String fileName = StringUtils.isBlank(zipFileName) ? FilenameUtils.getName(source) : zipFileName;
byte[] buffere = new byte[BUFFEREDSIZE];
File temp = new File(source);
try {
if (StringUtils.isBlank(source)) {
return;
} else {
if (StringUtils.isBlank(directory)) {
// 检查对象是否是文件夹(即目录)
if (temp.isDirectory()) {
directory = source + ".zip";
} else {
if (source.indexOf(".") > 0) {
directory = source.substring(0, source.lastIndexOf(".")) + ".zip";
} else {
directory = source + ".zip";
}
}
} else {
// 判断目标文件是否存在,若不存在则创建
File dirFile = new File(directory);
if (!dirFile.exists()) {
FileUtils.forceMkdir(dirFile);
}
if (fileName.indexOf(".") > 0) {
fileName = fileName.substring(0, fileName.indexOf("."));
}
directory += File.separator + fileName + ".zip";
}
}
// 递归获得该文件下所有文件名(不包括目录名)
List<File> fileList = FileUtilHelper.loadFileName(new File(source));
zos = new ZipOutputStream(new FileOutputStream(directory), ConstantHelper.GBK);
for (int i = 0; i < fileList.size(); i++) {
File file = (File) fileList.get(i);
zos.putNextEntry(new ZipEntry(getEntryName(source, file)));
bis = new BufferedInputStream(new FileInputStream(file));
int length = 0;
while ((length = bis.read(buffere)) != -1) {
zos.write(buffere, 0, length);
}
bis.close();
zos.closeEntry();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (zos != null) {
zos.close();
}
} catch (Exception e) {
}
}
}
Aggregations