Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef7d0463437848326900d653d0506b6b5b81075c (plain) (blame)
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
package org.eclipse.cdt.p2.internal.repo.artifact;

import java.io.File;
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.ZipInputStream;

public class ZipExtractor extends Thread {

	private final InputStream in;
	private final File installDir;
	private final FileListWriter fileListWriter;
	
	public ZipExtractor(InputStream in, File installDir, FileListWriter fileListWriter) {
		this.in = in;
		this.installDir = installDir;
		this.fileListWriter = fileListWriter;
	}

	@Override
	public void run() {
		try {
			ZipInputStream zipIn = new ZipInputStream(in);
			for (ZipEntry zipEntry = zipIn.getNextEntry(); zipEntry != null; zipEntry = zipIn.getNextEntry()) {
				File outFile = new File(installDir, zipEntry.getName());
				if (zipEntry.isDirectory()) {
					outFile.mkdirs();
				} else {
					if (outFile.exists())
						outFile.delete();
					else
						outFile.getParentFile().mkdirs();
					FileOutputStream outStream = new FileOutputStream(outFile);
					copyStream(zipIn, false, outStream, true);
					long lastModified = zipEntry.getTime();
					outFile.setLastModified(lastModified);
					fileListWriter.addFile(new InstalledFile(outFile, lastModified));
				}
				zipIn.closeEntry();
			}
			zipIn.close();
			fileListWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private static int copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut) throws IOException {
		try {
			int written = 0;
			byte[] buffer = new byte[1024];
			int len;
			while ((len = in.read(buffer)) != -1) {
				out.write(buffer, 0, len);
				written += len;
			}
			return written;
		} finally {
			try {
				if (closeIn) {
					in.close();
				}
			} finally {
				if (closeOut) {
					out.close();
				}
			}
		}
	}

}

Back to the top