- /**
- * 压缩文件
- *
- * @method zip_file
- * @param file_path 需压缩的文件路径
- */
- 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);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处
- 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();
- }
- }
- }
java压缩文件
最新推荐文章于 2021-02-26 10:18:38 发布