22
33
44import java .io .BufferedInputStream ;
5- import java .io .BufferedOutputStream ;
65import java .io .File ;
76import java .io .FileInputStream ;
87import java .io .FileOutputStream ;
1817import java .util .zip .ZipFile ;
1918import java .util .zip .ZipOutputStream ;
2019
21- import static com .blankj .utilcode .utils .ConstUtils .* ;
20+ import static com .blankj .utilcode .utils .ConstUtils .MB ;
2221
2322/**
2423 * <pre>
@@ -37,62 +36,152 @@ private ZipUtils() {
3736 /**
3837 * 批量压缩文件
3938 *
40- * @param resFileList 要压缩的文件列表
41- * @param zipFile 生成的压缩文件
39+ * @param resFiles 要压缩的文件列表
40+ * @param zipFilePath 压缩文件路径
41+ * @throws IOException IO错误时抛出
4242 */
43- public static void zipFiles (Collection <File > resFileList , File zipFile )
43+ public static void zipFiles (Collection <File > resFiles , String zipFilePath )
4444 throws IOException {
45- zipFiles (resFileList , zipFile , null );
45+ zipFiles (resFiles , zipFilePath , null );
4646 }
4747
4848 /**
4949 * 批量压缩文件
5050 *
51- * @param resFileList 要压缩的文件列表
52- * @param zipFile 生成的压缩文件
51+ * @param resFiles 要压缩的文件列表
52+ * @param zipFilePath 压缩文件路径
5353 * @param comment 压缩文件的注释
54+ * @throws IOException IO错误时抛出
5455 */
55- public static void zipFiles (Collection <File > resFileList , File zipFile , String comment ) {
56- try {
57- for (File resFile : resFileList ) {
58- zipFile (resFile , "" , zipFile , comment );
59- }
60- } catch (IOException e ) {
61- e .printStackTrace ();
56+ public static void zipFiles (Collection <File > resFiles , String zipFilePath , String comment )
57+ throws IOException {
58+ zipFiles (resFiles , FileUtils .getFileByPath (zipFilePath ), comment );
59+ }
60+
61+ /**
62+ * 批量压缩文件
63+ *
64+ * @param resFiles 要压缩的文件列表
65+ * @param zipFile 生成的压缩文件
66+ * @throws IOException IO错误时抛出
67+ */
68+ public static void zipFiles (Collection <File > resFiles , File zipFile )
69+ throws IOException {
70+ zipFiles (resFiles , zipFile , null );
71+ }
72+
73+ /**
74+ * 批量压缩文件
75+ *
76+ * @param resFiles 要压缩的文件列表
77+ * @param zipFile 压缩文件
78+ * @param comment 压缩文件的注释
79+ * @throws IOException IO错误时抛出
80+ */
81+ public static void zipFiles (Collection <File > resFiles , File zipFile , String comment )
82+ throws IOException {
83+ if (resFiles == null || zipFile == null ) return ;
84+ zipFiles (resFiles , new ZipOutputStream (new FileOutputStream (zipFile )), comment );
85+ }
86+
87+ /**
88+ * 批量压缩文件
89+ *
90+ * @param resFiles 要压缩的文件列表
91+ * @param zos 压缩文件输出流
92+ * @param comment 压缩文件的注释
93+ * @throws IOException IO错误时抛出
94+ */
95+ public static void zipFiles (Collection <File > resFiles , ZipOutputStream zos , String comment )
96+ throws IOException {
97+ for (File resFile : resFiles ) {
98+ zipFile (resFile , "" , zos , comment );
6299 }
63100 }
64101
65102 /**
66103 * 压缩文件
67104 *
68- * @param resDirPath 需要压缩的文件
69- * @param rootPath 相对于压缩文件的路径
70- * @param zipFile 压缩的目的文件
71- * @throws IOException 当压缩过程出错时抛出
105+ * @param resFilePath 需要压缩的文件路径
106+ * @param zipFilePath 压缩文件路径
107+ * @throws IOException IO错误时抛出
108+ */
109+ public static void zipFile (String resFilePath , String zipFilePath )
110+ throws IOException {
111+ zipFile (resFilePath , zipFilePath , null );
112+ }
113+
114+ /**
115+ * 压缩文件
116+ *
117+ * @param resFilePath 需要压缩的文件路径
118+ * @param zipFilePath 压缩文件路径
119+ * @param comment 压缩文件的注释
120+ * @throws IOException IO错误时抛出
121+ */
122+ public static void zipFile (String resFilePath , String zipFilePath , String comment )
123+ throws IOException {
124+ zipFile (FileUtils .getFileByPath (resFilePath ), FileUtils .getFileByPath (zipFilePath ), comment );
125+ }
126+
127+ /**
128+ * 压缩文件
129+ *
130+ * @param resFile 需要压缩的文件
131+ * @param zipFile 压缩文件
132+ * @throws IOException IO错误时抛出
133+ */
134+ public static void zipFile (File resFile , File zipFile )
135+ throws IOException {
136+ zipFile (resFile , zipFile , null );
137+ }
138+
139+ /**
140+ * 压缩文件
141+ *
142+ * @param resFile 需要压缩的文件
143+ * @param zipFile 压缩文件
144+ * @param comment 压缩文件的注释
145+ * @throws IOException IO错误时抛出
146+ */
147+ public static void zipFile (File resFile , File zipFile , String comment )
148+ throws IOException {
149+ if (resFile == null || zipFile == null ) return ;
150+ zipFile (resFile , "" , new ZipOutputStream (new FileOutputStream (zipFile )), comment );
151+ }
152+
153+ /**
154+ * 压缩文件
155+ *
156+ * @param resFile 需要压缩的文件
157+ * @param rootPath 相对于压缩文件的路径
158+ * @param zos 压缩文件输出流
159+ * @param comment 压缩文件的注释
160+ * @throws IOException IO错误时抛出
72161 */
73- public static void zipFile (File resDirPath , String rootPath , File zipFile , String comment )
162+ public static void zipFile (File resFile , String rootPath , ZipOutputStream zos , String comment )
74163 throws IOException {
75- rootPath = rootPath + (rootPath . trim (). length () == 0 ? "" : File .separator ) + resDirPath .getName ();
76- if (resDirPath .isDirectory ()) {
77- File [] fileList = resDirPath .listFiles ();
164+ rootPath = rootPath + (StringUtils . isSpace ( rootPath ) ? "" : File .separator ) + resFile .getName ();
165+ if (resFile .isDirectory ()) {
166+ File [] fileList = resFile .listFiles ();
78167 for (File file : fileList ) {
79- zipFile (file , rootPath , file , comment );
168+ zipFile (file , rootPath , zos , comment );
80169 }
81170 } else {
82171 InputStream is = null ;
83- ZipOutputStream zos = null ;
84172 try {
85- is = new BufferedInputStream (new FileInputStream (resDirPath ));
86- zos = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile )));
173+ is = new BufferedInputStream (new FileInputStream (resFile ));
87174 zos .putNextEntry (new ZipEntry (rootPath ));
88175 byte buffer [] = new byte [MB ];
89176 int len ;
90177 while ((len = is .read (buffer )) != -1 ) {
91178 zos .write (buffer , 0 , len );
92179 }
93- if (comment != null ) zos .setComment (comment );
180+ if (StringUtils . isEmpty ( comment ) ) zos .setComment (comment );
94181 } finally {
95- FileUtils .closeIO (is , zos );
182+ FileUtils .closeIO (is );
183+ zos .flush ();
184+ zos .closeEntry ();
96185 }
97186 }
98187 }
@@ -104,7 +193,7 @@ public static void zipFile(File resDirPath, String rootPath, File zipFile, Strin
104193 * @param destDirPath 解压缩的目标目录
105194 * @throws IOException IO错误时抛出
106195 */
107- public static void upZipFile (String zipFilePath , String destDirPath )
196+ public static void unzipFile (String zipFilePath , String destDirPath )
108197 throws IOException {
109198 if (!FileUtils .createOrExistsDir (destDirPath )) return ;
110199 ZipFile zf = new ZipFile (zipFilePath );
@@ -142,7 +231,7 @@ public static void upZipFile(String zipFilePath, String destDirPath)
142231 * @param targetFile 目标文件名
143232 * @throws IOException IO错误时抛出
144233 */
145- public static List <File > upZipSelectedFile (File zipFile , String destDirPath , String targetFile )
234+ public static List <File > unzipSelectedFile (File zipFile , String destDirPath , String targetFile )
146235 throws IOException {
147236 if (!FileUtils .createOrExistsDir (destDirPath )) return null ;
148237 List <File > fileList = new ArrayList <>();
0 commit comments