package com
.chuangqi
.tools
;
import java
.io
.File
;
import java
.io
.FileInputStream
;
import java
.io
.FileNotFoundException
;
import java
.io
.IOException
;
import java
.util
.zip
.ZipEntry
;
import java
.util
.zip
.ZipOutputStream
;
public class ZipUtils
{
public
static void zipDir(String directoryName
, ZipOutputStream zos
, String basePath
) {
File file
= new
File(directoryName
);
String copyBasePath
="";
if (file
.exists()) {
File
[] fileList
= file
.listFiles();
for (File f
: fileList
) {
if (f
.isDirectory()) {
if (!"".equals(basePath
)) {
copyBasePath
= basePath
+ File
.separator
+f
.getName();
} else {
copyBasePath
= f
.getName();
}
zipDir(directoryName
+ File
.separator
+ f
.getName(), zos
, copyBasePath
);
} else {
String zipName
;
if (!"".equals(basePath
)) {
zipName
= basePath
+ File
.separator
+ f
.getName();
} else {
zipName
= f
.getName();
}
try
{
zos
.putNextEntry(new
ZipEntry(zipName
));
int len
;
FileInputStream is
= new
FileInputStream(f
);
byte
[] bytes
= new byte
[1024];
while ((len
= is
.read(bytes
)) != -1) {
zos
.write(bytes
, 0, len
);
}
zos
.flush();
zos
.closeEntry();
is
.close();
} catch
(FileNotFoundException e
) {
e
.printStackTrace();
} catch
(IOException e
) {
e
.printStackTrace();
}
}
}
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-7161.html