|
@@ -21,6 +21,8 @@ import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
@@ -191,48 +193,37 @@ public class FileDisposeUtil {
|
|
|
zos = new ZipOutputStream(new BufferedOutputStream(fos));
|
|
|
byte[] bufs = new byte[1024 * 10];
|
|
|
for (int i = 0; i < sourceFiles.length; i++) {
|
|
|
- // 创建ZIP实体,并添加进压缩包
|
|
|
- String fileEncode = System.getProperty("file.encoding");
|
|
|
- String name = new String(sourceFiles[i].getName().getBytes(fileEncode),"UTF-8");
|
|
|
- ZipEntry zipEntry = new ZipEntry(name);
|
|
|
- zos.putNextEntry(zipEntry);
|
|
|
- // 读取待压缩的文件并写进压缩包里
|
|
|
- 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);
|
|
|
+ try{
|
|
|
+ //创建ZIP实体,并添加进压缩包
|
|
|
+ String fileEncode = System.getProperty("file.encoding");
|
|
|
+ String name = new String(sourceFiles[i].getName().getBytes(fileEncode),"UTF-8");
|
|
|
+ ZipEntry zipEntry = new ZipEntry(name);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+ //读取待压缩的文件并写进压缩包里
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ zos.flush();
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ IOUtils.closeQuietly(bis);
|
|
|
+ IOUtils.closeQuietly(fis);
|
|
|
}
|
|
|
- zos.flush();
|
|
|
}
|
|
|
flag = true;
|
|
|
}
|
|
|
}
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
- try {
|
|
|
- if (null != bis){
|
|
|
- bis.close();
|
|
|
- bis = null;
|
|
|
- }
|
|
|
- if (null != fis){
|
|
|
- fis.close();
|
|
|
- fis = null;
|
|
|
- }
|
|
|
- if (null != zos){
|
|
|
- zos.close();
|
|
|
- zos = null;
|
|
|
- }
|
|
|
- if (null != fos){
|
|
|
- fos.close();
|
|
|
- fos = null;
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ IOUtils.closeQuietly(bis);
|
|
|
+ IOUtils.closeQuietly(fis);
|
|
|
+ IOUtils.closeQuietly(zos);
|
|
|
+ IOUtils.closeQuietly(fos);
|
|
|
}
|
|
|
}
|
|
|
logger.info("压缩"+sourceFilePath+"目录完成");
|
|
@@ -241,13 +232,11 @@ public class FileDisposeUtil {
|
|
|
|
|
|
public static void createDirectory(String downloadDirectory) {
|
|
|
File directory = new File(downloadDirectory);
|
|
|
- if(directory.exists()){
|
|
|
- File[] files = directory.listFiles();
|
|
|
- for(File file:files){
|
|
|
- file.delete();
|
|
|
- }
|
|
|
+ if(!directory.exists()){
|
|
|
+ directory.mkdirs();
|
|
|
}else{
|
|
|
- directory.mkdir();
|
|
|
+ FileUtils.deleteQuietly(directory);
|
|
|
+ directory.mkdirs();
|
|
|
}
|
|
|
}
|
|
|
|