激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java壓縮解壓縮工具類

Java壓縮解壓縮工具類

2021-06-22 13:25一掬凈土 Java教程

這篇文章主要為大家詳細介紹了Java壓縮解壓縮工具類,如何壓縮單個文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java壓縮解壓縮工具類的具體代碼,供大家參考,具體內容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.wdy.tools.utils.pressuitl;
 
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.zip.zipentry;
import java.util.zip.zipfile;
import java.util.zip.zipinputstream;
import java.util.zip.zipoutputstream;
 
import org.apache.commons.logging.log;
 
import com.wdy.tools.utils.logutil;
 
/**
 * 壓縮/解壓縮工具類(zip格式)
 *
 * @author wdy
 * @date 2016-08-23
 */
public class pressutil {
 
 private static final log log = logutil.getlog(pressutil.class);
 
 
 
 public static void main(string[] args) {
// pressutil.zipmultifile("d:\\nwp_trans\\nwp_h\\", "d:\\nwp_trans\\nwp_h\\wdy.zip");
 string sourcefilepath = "d:\\nwp_trans\\nwp_h\\";
   string zipfilepath = "d:\\nwp_trans\\nwp_h\\";
   string filename = "wdy";
   boolean flag = pressutil.filetozip(sourcefilepath, zipfilepath, filename);
   if(flag){
   log.info("文件打包成功!");
   }else{
   log.info("文件打包失敗!");
   }
 }
 
 /**
  * 將存放在sourcefilepath目錄下的源文件,打包成filename名稱的zip文件,并存放到zipfilepath路徑下
  * @param sourcefilepath :待壓縮的文件路徑
  * @param zipfilepath :壓縮后存放路徑
  * @param filename :壓縮后文件的名稱(不包括擴展名)
  * @return
  */
 @suppresswarnings("resource")
 public static boolean filetozip(string sourcefilepath,string zipfilepath,string filename){
  boolean flag = false;
  file sourcefile = new file(sourcefilepath);
  fileinputstream fis = null;
  bufferedinputstream bis = null;
  fileoutputstream fos = null;
  zipoutputstream zos = null;
   
  if(sourcefile.exists() == false){
   log.info("待壓縮的文件目錄:"+sourcefilepath+"不存在.");
  }else{
   try {
    file zipfile = new file(zipfilepath + "/" + filename +".zip");
    if(zipfile.exists()){
     log.info(zipfilepath + "目錄下存在名字為:" + filename +".zip" +"打包文件.");
    }else{
     file[] sourcefiles = sourcefile.listfiles();
     if(null == sourcefiles || sourcefiles.length<1){
      log.info("待壓縮的文件目錄:" + sourcefilepath + "里面不存在文件,無需壓縮.");
     }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++){
       //創建zip實體,并添加進壓縮包
       zipentry zipentry = new zipentry(sourcefiles[i].getname());
       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);
       }
      }
      flag = true;
     }
    }
   } 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 flag;
 }
 
 /**
 * 壓縮單個文件
 * @param filepath 要被壓縮的文件的全路徑,包含文件名d:/hello.txt
 * @param zippath 壓縮后的全路徑,包含文件名d:/hello.zip
 */
 public static void ziponefile(string filepath, string zippath) {
 try {
 file file = new file(filepath);
 file zipfile = new file(zippath);
 inputstream input = new fileinputstream(file);
 zipoutputstream zipout = new zipoutputstream(new fileoutputstream(zipfile));
 zipout.putnextentry(new zipentry(file.getname()));
 int temp = 0;
 while ((temp = input.read()) != -1) {
 zipout.write(temp);
 }
 input.close();
 zipout.close();
 } catch (exception e) {
 e.printstacktrace();
 }
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/wdy_2099/article/details/72916282

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天艹综合 | 成年人黄视频 | 欧美日韩免费一区二区三区 | 美女擦逼 | 激情综合在线 | 欧美日韩综合视频 | 精品国产一区二区三区天美传媒 | 失禁高潮抽搐喷水h | 久久久精品视频国产 | 国产亚洲精品久久久久婷婷瑜伽 | 精品成人在线观看 | 九色激情网 | 肉文女配h | 免费午夜网站 | 精品一区二区久久久久久久网精 | 久久网站免费 | 亚洲国产网址 | 免费观看的毛片手机视频 | 18pao国产成人免费视频 | 一级成人毛片 | 一区二区三区小视频 | 国产亚洲区 | 国产日产精品久久久久快鸭 | 久久网站免费 | chinese 军人 gay xx 呻吟 | 久久久久久久久浪潮精品 | 国产羞羞视频在线观看免费应用 | 国产精品视频一区二区三区四区五区 | 日韩精品一区二区三区中文 | 国产大片中文字幕在线观看 | 一区二区三区欧美日韩 | 色的综合 | 久久精品污 | 日韩视频一二区 | 国产一级一片免费播放 | 久久性生活免费视频 | 久在线观看 | 91福利影视 | 亚洲精品成人18久久久久 | 欧美精品一区二区三区在线 | 亚洲日本韩国精品 |