1 package com.liveyc.common.utils;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.zip.GZIPOutputStream;
9
10 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
11 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
12 import org.apache.commons.compress.utils.IOUtils;
13
14 /**
15 * GZIPUtil
16 * @Description:
17 * @author: xuyou
18 * @date: 2017年11月8日
19 */
20 public class GZIPUtil {
21
22
23 /**
24 * @Title: pack
25 * @Description: 将一组文件打成tar包
26 * @param sources 要打包的原文件数组
27 * @param target 打包后的文件
28 * @return File 返回打包后的文件
29 * @throws
30 */
31 public static File pack(File[] sources, File target) {
32 FileOutputStream out = null;
33 try {
34 out = new FileOutputStream(target);
35 } catch (FileNotFoundException e1) {
36 e1.printStackTrace();
37 }
38 TarArchiveOutputStream os = new TarArchiveOutputStream(out);
39 for (File file : sources) {
40 try {
41 os.putArchiveEntry(new TarArchiveEntry(file));
42 IOUtils.copy(new FileInputStream(file), os);
43 os.closeArchiveEntry();
44
45 } catch (FileNotFoundException e) {
46 e.printStackTrace();
47 } catch (IOException e) {
48 e.printStackTrace();
49 }
50 }
51 if (os != null) {
52 try {
53 os.flush();
54 os.close();
55 } catch (IOException e) {
56 e.printStackTrace();
57 }
58 }
59
60 return target;
61 }
62
63 /**
64 *
65 * @Title: compress
66 * @Description: 将文件用gzip压缩
67 * @param source 需要压缩的文件
68 * @return File 返回压缩后的文件
69 * @throws
70 */
71 public static void compress(File source,String path) {
72 File target = new File(path);
73 FileInputStream in = null;
74 GZIPOutputStream out = null;
75 try {
76 in = new FileInputStream(source);
77 out = new GZIPOutputStream(new FileOutputStream(target));
78 byte[] array = new byte[1024];
79 int number = -1;
80 while ((number = in.read(array, 0, array.length)) != -1) {
81 out.write(array, 0, number);
82 }
83 } catch (FileNotFoundException e) {
84 e.printStackTrace();
85 } catch (IOException e) {
86 e.printStackTrace();
87 } finally {
88 if (in != null) {
89 try {
90 in.close();
91 } catch (IOException e) {
92 e.printStackTrace();
93 }
94 }
95
96 if (out != null) {
97 try {
98 out.close();
99 } catch (IOException e) {
100 e.printStackTrace();
101 }
102 }
103 }
104 }
105
106 /**
107 * @Description: 将文件夹压缩成gz格式
108 * @Title: compression
109 * @author: xuyou
110 * @date: 2017年11月9日
111 * @param source
112 * @throws FileNotFoundException
113 * @throws IOException
114 */
115 public static void compression(String source) throws FileNotFoundException, IOException{
116 File[] sources = new File(source).listFiles();
117 File file = new File(source+"test"+".gz");
118 File pack = pack(sources, file);
119 compress(pack,source+".har.gz");
120 System.gc();
121 pack.delete();
122 CompressedFileUtil.deleteFile(source);
123 }
124
125
126 /**
127 * 读取某个文件夹下的所有文件
128 */
129 public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
130 try {
131 File file = new File(filepath);
132 if (!file.isDirectory()) {
133 System.out.println("path=" + file.getPath());
134 } else if (file.isDirectory()) {
135 String[] filelist = file.list();
136 for (int i = 0; i < filelist.length; i++) {
137 File readfile = new File(filepath + "\\" + filelist[i]);
138 if (!readfile.isDirectory()) {
139 System.out.println("path=" + readfile.getPath());
140 } else if (readfile.isDirectory()) {
141 readfile(filepath + "\\" + filelist[i]);
142 }
143 }
144 }
145
146 } catch (FileNotFoundException e) {
147 System.out.println("readfile() Exception:" + e.getMessage());
148 }
149 return true;
150 }
151
152
153 /**
154 * 删除某个文件夹下的所有文件夹和文件
155 * @param delpath
156 * @return
157 * @throws FileNotFoundException
158 * @throws IOException
159 */
160 public static boolean deleteFile(String delpath)
161 throws FileNotFoundException, IOException {
162 try {
163 File file = new File(delpath);
164 if (!file.isDirectory()) {
165 file.delete();
166 } else if (file.isDirectory()) {
167 File[] fileList = file.listFiles();
168 for (int i = 0; i < fileList.length; i++) {
169 File delfile = fileList[i];
170 if (!delfile.isDirectory()) {
171 /*System.out.println("相对路径=" + delfile.getPath());
172 System.out.println("绝对路径=" + delfile.getAbsolutePath());
173 System.out.println("文件全名=" + delfile.getName());
174 System.out.println("删除文件成功");*/
175 delfile.delete();
176 } else if (delfile.isDirectory()) {
177 deleteFile(fileList[i].getPath());
178 }
179 }
180 file.delete();
181 }
182 } catch (FileNotFoundException e) {
183 System.out.println("deletefile() Exception:" + e.getMessage());
184 }
185 return true;
186 }
187
188
189
190
191 public static void main(String[] args) throws FileNotFoundException, IOException {
192
193 File[] sources = new File("D:\\test2.har").listFiles();
194 // System.out.println(files);
195 // File[] sources = new File[] { file };
196 File target = new File("D:\\test.gz");
197 // pack(sources, target);
198 File pack = pack(sources, target);
199 compress(pack,"D:\\test.har.gz");
200 System.gc();
201 CompressedFileUtil.deleteFile("D:\\test2.har");
202
203
204 }
205 }