summaryrefslogtreecommitdiffstats
path: root/src/test/util/ZipStripper.java
blob: 2eeb2c06c2fd787a2797d7530334608ea12cd762 (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
package test.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipStripper {

	public static void main(String[] args) {

		try {
			String sourceFileName = args[0];
			File sourceFile = new File(sourceFileName);
			
			File tempFile = new File(sourceFile.getParentFile(), "tmp31415926535.zip");
			tempFile.createNewFile();
			
			ZipOutputStream outTemp = new ZipOutputStream(new FileOutputStream(tempFile));
			
			ZipFile archive = new ZipFile(sourceFile);
			
			Enumeration<? extends ZipEntry> en = archive.entries();
			while(en.hasMoreElements()) {
				ZipEntry entr = en.nextElement();
			
				outTemp.putNextEntry(new ZipEntry(entr.getName()));
				
				if(!entr.isDirectory()) {
					InputStream in = archive.getInputStream(entr);
					
					copyInputStream(in, outTemp);
					in.close();
				}
			}
			
			outTemp.flush();
			outTemp.close();
			
			archive.close();
			
			String destFileName = args[1];

			if(sourceFileName.equals(destFileName)) {
				sourceFile.delete();
			}
			
			tempFile.renameTo(new File(destFileName));
			
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		
	}

	public static void copyInputStream(InputStream in, OutputStream out)throws IOException {
		
		byte[] buffer = new byte[1024];
		int len;
		
		while((len = in.read(buffer)) >= 0) {
			out.write(buffer, 0, len);
		}
	}
	
}