public String zip(String fileName) {
String zipName=UUID.randomUUID().toString();//压缩以后的文件名
String zipPath = "/mnt/fileserver"; //压缩以后文件的存放路径
String sourcePath="/mnt/fileserver/upload";//要压缩的文件的路径
File sourceFile = new File(sourcePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if (sourceFile.exists() == false) {
System.out.println("File catalog:" + sourcePath + "not exist!");
} else {
try {
if(!new File(zipPath).exists()){
new File(zipPath).mkdirs();
}
File zipFile = new File(zipPath + "/" + zipName + ".zip");
System.out.println(zipFile);
if (zipFile.exists()) {
System.out.println(zipPath + "Catalog File: " + zipName + ".zip" + "pack file.");
} else {
File[] sourceFiles = sourceFile.listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
System.out.println("File Catalog:" + sourcePath + "nothing in there,don't hava to compress!");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 10];
for (int i = 0; i < sourceFiles.length; i++) {
if(sourceFiles[i].getName().equals(fileName)){
// create .zip and put pictures in
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
// read documents and put them in the zip
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
try {
if (null != bis)
bis.close();
if (null != zos)
zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return zipName;
}