summaryrefslogtreecommitdiff
path: root/src/com/nis/nmsclient/util/ZipUtil.java
blob: ee8e984385fbe7e438950f2943a05182e8fa316c (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.nis.nmsclient.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.ServerCollectData;

public class ZipUtil {
	static Logger logger = Logger.getLogger(ZipUtil.class);
	/**
	 * java 解压缩单个zip压缩包 存在文件和目录的汉字处理问题
	 * 
	 * @param zipFileName 需要解压的压缩包文件
	 * @param destDir 解压缩后的目标路径
	 * @throws Exception 
	 */
	public static void unZip(String zipFileName, String destDir) throws Exception {
		logger.debug("unZip start……");
		ZipFile zipFile = null;
		try{
			zipFile = new ZipFile(zipFileName);
			Enumeration en = zipFile.getEntries();
			ZipEntry zipEntry = null;
			while (en.hasMoreElements()) {
				zipEntry = (ZipEntry) en.nextElement();
				String path = (destDir + File.separator + zipEntry.getName()).replaceAll("\\\\", "/");
				if (zipEntry.isDirectory()) {// 如果得到的是目录
					int end = path.lastIndexOf("/");
					if (end != -1) {
						File dir = new File(path.substring(0, end));
						if (!dir.exists()) {
							dir.mkdir();
						}
					}
				} else {
					InputStream in = null;
					FileOutputStream out = null;
					try{
						File f = new File(path);
						if(!f.getParentFile().exists()){
							f.getParentFile().mkdirs(); 
						}
		                f.createNewFile();  
						in = zipFile.getInputStream(zipEntry);
						out = new FileOutputStream(f);
						IOUtils.copy(in, out);
					}catch (Exception e) {
						throw e;
					}finally{
						if(in!=null) in.close();
						if(out!=null) out.close();
					}
				}
			}// while end
		} catch (Exception e) {
			throw e;
		}finally {
			if(zipFile != null){
				zipFile.close();
				zipFile = null;
			}
		}
		logger.debug("unZip end……");
	}
	
	/**
	 * 将单个文件或一个目录打成zip包
	 * 
	 * @param srcDir 需要压压缩的文件目录
	 * @param destFile 压缩后的目标文件
	 * @throws Exception 
	 */
	public static void zip(String srcDir, String destFile, String[] excludes) throws Exception {  
		ZipOutputStream out = null;
		try{
			out = new ZipOutputStream(new FileOutputStream(destFile));  
			//out.setEncoding("GBK");//解决linux乱码
	        File srcFile = new File(srcDir);
	        if (!srcFile.exists()) {
//	            throw new Exception("压缩目录或文件不存在: " + srcFile.getAbsolutePath());
	            throw new Exception("Compressed directories or files do not exist: " + srcFile.getAbsolutePath());
	        }
	        if(excludes!=null && excludes.length>0){
	        	for (int i=0; i<excludes.length; i++) {
	        		File excudeFile = new File(excludes[i]);
	        		excludes[i] = excudeFile.getCanonicalPath();
				}
	        }
			
	       /* if(srcFile.isDirectory()){
	        	zip(out,srcFile,srcFile.getName(), excludes); 
	        }else{
	        	zip(out,srcFile,"", excludes);
	        }*/
	        zip(out,srcFile,"", excludes);
        }catch (Exception e) {
        	throw e;
        }finally {
        	if(out!=null)  out.close();
        }
    }  
      
     /**
      * zip压缩
      * @param out: Zip压缩流
      * @param srcFile: 要压缩的目录或文件
      * @param base: 要压缩文件在zip包内的路径
      * @throws Exception
    */
    protected static void zip(ZipOutputStream out, File srcFile, String base, String[] excludes) throws Exception{
        if (!srcFile.exists()) {
//            throw new Exception("压缩目录或文件不存在: " + srcFile.getAbsolutePath());
            throw new Exception("Compressed directories or files do not exist: " + srcFile.getAbsolutePath());
        }
        
        if(excludes!=null && excludes.length>0){
    		for(String exclude : excludes){
    			if(exclude.equalsIgnoreCase(srcFile.getCanonicalPath())){
    				return;
    			}
    		}
    	}
        
        if (srcFile.isDirectory()) {
            File[] files = srcFile.listFiles();
            base = base.length() == 0 ? "" : base + "/";
            if (base.length() > 0) {
                out.putNextEntry(new ZipEntry(base));
               /* ZipEntry zipEntry = new ZipEntry(base);
				zipEntry.setUnixMode(755);// 解决linux乱码
				out.putNextEntry(zipEntry);*/
            }
            for (int i = 0; i < files.length; i++) {
                zip(out, files[i], base + files[i].getName(), excludes);
            }
        } else {
            base = base.length() == 0 ? srcFile.getName() : base;
            out.putNextEntry(new ZipEntry(base));
            /*ZipEntry zipEntry=new ZipEntry(base);
            zipEntry.setUnixMode(644);//解决linux乱码
            out.putNextEntry(zipEntry);*/
            FileInputStream fis = null;
            try{
	            fis = new FileInputStream(srcFile);
				IOUtils.copy(fis, out);
            }catch (IOException e) {
				throw e;
			}finally{
				if(fis!=null) fis.close();
			}
        }
        out.closeEntry();
    }
    
    /**
	 * 将单个文件或一个目录打成zip包,并将原文件删除
	 * 
	 * @param srcFiles 需要压压缩的文件列表
	 * @param destFile 压缩后的目标文件
	 * @param isAddPrefix 是否要添加文件前缀
	 * @throws Exception 
	 */
	public static void zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix) throws Exception {
		logger.debug("pass ZipUtil zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix):");
		if(srcFiles!=null){
			logger.debug("ZipUtil zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix) srcFiles:"+srcFiles.length);
		}
		ZipOutputStream out = null;
		try{
			out = new ZipOutputStream(new FileOutputStream(destFile));  
			//out.setEncoding("GBK");//解决linux乱码
	        if (srcFiles==null || srcFiles.length==0) {
//	            throw new Exception("压缩文件列表为空");
	            throw new Exception("The list of compressed files is empty");
	        }
	        for(File file : srcFiles){
	        	zip(out,file,"", true, isAddPrefix, null);
	        }
        }catch (Exception e) {
        	throw e;
        }finally {
        	if(out!=null)  out.close();
        }
    }
	
	/**
	 * 将单个文件或一个目录打成zip包,并将原文件删除或移动
	 * 
	 * @param srcFiles 需要压压缩的文件列表
	 * @param destFile 压缩后的目标文件
	 * @param isAddPrefix 是否要添加文件前缀
	 * @throws Exception 
	 */
	public static void zipWithMoveFile(File[] srcFiles, String destFile, boolean isAddPrefix) throws Exception {
		ZipOutputStream out = null;
		try{
			out = new ZipOutputStream(new FileOutputStream(destFile));  
			//out.setEncoding("GBK");//解决linux乱码
	        if (srcFiles==null || srcFiles.length==0) {
//	            throw new Exception("压缩文件列表为空");
	            throw new Exception("The list of compressed files is empty");
	        }
	        // 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
	        String dataType = null;
	        String destFileName = new File(destFile).getName();
	        if(destFileName.startsWith(CommonSocket.BP_TYPE_TASK_RESULT)){
	        	dataType = CommonSocket.BP_TYPE_TASK_RESULT;
	        }else if(destFileName.startsWith(CommonSocket.BP_TYPE_TASK_RETURN)){
	        	dataType = CommonSocket.BP_TYPE_TASK_RETURN;
	        }else if(destFileName.startsWith(CommonSocket.BP_TYPE_DETECT_DATA)){
	        	dataType = CommonSocket.BP_TYPE_DETECT_DATA;
	        }
	        logger.debug("zipWithMoveFile --- dataType = " + dataType);
	        for(File file : srcFiles){
	        	// 2013-5-6 针对监测数据打包文件个数的限制,即srcFiles传来的是一定个数的文件,不是所有监测的目录,所以压缩文件中的文件名要加上文件的父目录名
	        	if(CommonSocket.BP_TYPE_DETECT_DATA.equalsIgnoreCase(dataType) && file.isFile()){
	        		zip(out, file, file.getParentFile().getName() + "_" + file.getName(), true, isAddPrefix, dataType);
	        	}else{
	        		zip(out,file,"", true, isAddPrefix, dataType);
	        	}
	        }
        }catch (Exception e) {
        	throw e;
        }finally {
        	if(out!=null)  out.close();
        }
    }  
	
    /**
     * zip压缩
     * @param out: Zip压缩流
     * @param srcFile: 要压缩的文件或目录
     * @param base: 要压缩文件在zip包内的路径
     * @param isDel 是否删除压缩的文件, 若dataType为null,则直接删除,若dataType不为null, 则移动到指定目录
     * @param isAddPrefix 是否要添加文件前缀,true 如果是文件则将父文件夹的名称加到文件名的前缀,作为压缩后的文件名
     * @throws Exception
     */
    protected static void zip(ZipOutputStream out, File srcFile, String base, boolean isDel, boolean isAddPrefix, String dataType) throws Exception{
        if (!srcFile.exists()) {
//            throw new Exception("压缩目录或文件不存在: " + srcFile.getAbsolutePath());
            throw new Exception("Compressed directories or files do not exist: " + srcFile.getAbsolutePath());
        }
        logger.debug("pass ZipUtil zip");
		
        if (srcFile.isDirectory()) {
            File[] files = srcFile.listFiles();
            base = base.length() == 0 ? "" : base + "/";
            if (base.length() > 0) {
                out.putNextEntry(new ZipEntry(base));
               /* ZipEntry zipEntry = new ZipEntry(base);
				zipEntry.setUnixMode(755);// 解决linux乱码
				out.putNextEntry(zipEntry);*/
            }
            for (int i = 0; i < files.length; i++) {
            	String fileName = files[i].getName();
            	if(isAddPrefix){
            		fileName = files[i].getParentFile().getName() + "_" + files[i].getName();
            	}
                zip(out, files[i], base + fileName, isDel, isAddPrefix, dataType);
            }
        } else{
            base = base.length() == 0 ? srcFile.getName() : base;
            out.putNextEntry(new ZipEntry(base));
            /*ZipEntry zipEntry=new ZipEntry(base);
            zipEntry.setUnixMode(644);//解决linux乱码
            out.putNextEntry(zipEntry);*/
            FileInputStream fis = null;
            try{
	            fis = new FileInputStream(srcFile);
				IOUtils.copy(fis, out);
            }catch (IOException e) {
				throw e;
			}finally{
				if(fis!=null) fis.close();
			}
			if(srcFile.exists() && isDel){
				 // 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
				if(CommonSocket.BP_TYPE_TASK_RESULT.equalsIgnoreCase(dataType)){
					ServerCollectData.moveTaskResultToDateDir(srcFile);
				}else if(CommonSocket.BP_TYPE_TASK_RETURN.equalsIgnoreCase(dataType)){
					ServerCollectData.moveTaskReturnToDateDir(srcFile, null);
				}else if(CommonSocket.BP_TYPE_DETECT_DATA.equalsIgnoreCase(dataType)){
					ServerCollectData.moveDetecDataToDateDir(srcFile);
				}else {
					FileUtil.delDir(srcFile);
		            logger.debug("ZipUtil.zip()----delete srcFile=" + srcFile.getAbsolutePath());
				}
	        }
        }
        out.closeEntry();
    }

	public static void main(String[] args) {
		try {
			File f = new File("D:/work/create_tablespace");
			System.out.println(f.getPath());
			long tt = System.currentTimeMillis();
			/*String[] excludes = new String[3];
			excludes[0] = "D:\\temp\\t1\\test\\src";
			excludes[1] = "D:\\temp\\t1\\test\\WebRoot\\WEB-INF\\lib\\jdom.jar";
			excludes[2] = "D:\\temp\\t1\\test\\ziptest_code.zip";
			ZipUtil.zip("D:\\temp\\t1", "D:\\temp\\test\\t1.zip", excludes);
			//ZipUtil.unZip("D:\\temp\\test\\ziptest_code.zip", "D:\\temp");
			System.out.println(System.currentTimeMillis()-tt);*/
			ZipUtil.zipWithDelFile(new File("D:\\nmstest\\logs\\").listFiles(), "D:\\test\\nmsserver.log.zip", true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}