java压缩文件
来源:互联网 发布:mac卸载第三方软件 编辑:程序博客网 时间:2024/05/19 21:01
-
-
-
-
-
-
- private void zip_file(String file_path){
- File file = new File(file_path);
- FileInputStream fileInputStream = null;
- BufferedInputStream bufferedInputStream = null;
- ZipOutputStream zipOutputStream = null;
- try {
- fileInputStream = new FileInputStream(file);
- bufferedInputStream = new BufferedInputStream(fileInputStream);
- byte[] buf = new byte[1024];
- int len;
- FileOutputStream fileOutputStream = new FileOutputStream(GlobalVar.serverConfig.getProperty("vasserver.adcftp.srcdir")+file.getName()+".ZIP");
- BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
- zipOutputStream = new ZipOutputStream(bos);
- ZipEntry zipEntry = new ZipEntry(file.getName());
- zipOutputStream.putNextEntry(zipEntry);
- while(-1 != (len=bufferedInputStream.read(buf))){
- zipOutputStream.write(buf, 0, len);
- zipOutputStream.flush();
- }
-
- } catch (FileNotFoundException e) {
- log.error("未找到需压缩的文件!-->" + e.getMessage());
- e.printStackTrace();
- System.out.println("未找到需压缩的文件!-->" + e.getMessage());
- } catch (IOException e) {
- log.error("文件压缩异常!-->" + e.getMessage());
- e.printStackTrace();
- System.out.println("文件压缩异常!-->" + e.getMessage());
- } finally {
- try {
- if(null != zipOutputStream)
- zipOutputStream.close();
- if(null != bufferedInputStream)
- bufferedInputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }