1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.jadira.repositorysnapshot;
19
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.zip.Deflater;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipException;
28 import java.util.zip.ZipOutputStream;
29
30 import org.jadira.dependencynavigator.gui.ProgressMeter;
31 import org.jadira.dependencynavigator.implementations.zip.ZipFileRepository;
32 import org.jadira.dependencynavigator.implementations.zip.ZipFileWorkspace;
33
34 public class RepositorySnapshot {
35
36 private final File snapshotFile;
37 private final File repositoryRoot;
38 private final File workspace;
39
40 public RepositorySnapshot(File repositoryRoot, File snapshotFile, File workspace) {
41 this.repositoryRoot = repositoryRoot;
42 this.snapshotFile = snapshotFile;
43 this.workspace = workspace;
44 }
45
46 public void createSnapshot(ProgressMeter progressMeter) throws ZipException, IOException {
47 snapshotFile.delete();
48 snapshotFile.getParentFile().mkdirs();
49 snapshotFile.createNewFile();
50 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(snapshotFile)));
51 out.setLevel(Deflater.BEST_COMPRESSION);
52 out.setMethod(ZipOutputStream.DEFLATED);
53
54 HierarchyFilter workspaceFilter = new HierarchyFilter() {
55
56 @Override
57 public boolean includeDirectory(File directory) throws IOException {
58 if (directory.getName().equals(".metadata")) {
59 return false;
60 }
61 if (directory.getCanonicalPath().equals(repositoryRoot.getCanonicalPath())) {
62 return false;
63 }
64 return true;
65 }
66
67 };
68
69 addHeirarchy(workspace, out, ZipFileWorkspace.PREFIX_WORKSPACE, workspaceFilter, progressMeter);
70
71 HierarchyFilter repositoryFilter = new HierarchyFilter() {
72
73 @Override
74 public boolean includeFile(File file) throws IOException {
75 if (!file.getName().endsWith(".pom")) {
76
77 return false;
78 }
79 if (file.getName().matches("^.*-\\d{8}\\.\\d{6}-\\d+\\.pom$")) {
80
81 return false;
82 }
83 return true;
84 }
85
86 };
87
88 addHeirarchy(repositoryRoot, out, ZipFileRepository.PREFIX_REPOSITORY, repositoryFilter, progressMeter);
89 out.close();
90 }
91
92 private void addHeirarchy(File directory, ZipOutputStream out, String repoPath, HierarchyFilter filter, ProgressMeter progressMeter) throws IOException {
93 File[] files = directory.listFiles();
94 for (int i = 0; i < files.length; i++) {
95 File file = files[i];
96 if (file.isDirectory() && filter.includeDirectory(file)) {
97 progressMeter.scanning(file);
98 addHeirarchy(file, out, repoPath + "/" + file.getName(), filter, progressMeter);
99 } else {
100 if (filter.includeFile(file)) {
101 progressMeter.adding(file);
102 addEntry(file, repoPath + "/" + file.getName(), out);
103 }
104 }
105 }
106 }
107
108 private class HierarchyFilter {
109
110 public boolean includeFile(File file) throws IOException {
111 return file.getName().equals("pom.xml");
112 }
113
114 public boolean includeDirectory(File directory) throws IOException {
115 return true;
116 }
117 }
118
119 private void addEntry(File target, String repoPath, ZipOutputStream out) throws IOException {
120 ZipEntry entry = new ZipEntry(repoPath);
121 int fileLength = (int) target.length();
122 FileInputStream fis = new FileInputStream(target);
123 byte[] wholeFile = new byte[fileLength];
124 fis.read(wholeFile, 0, fileLength);
125 fis.close();
126 out.putNextEntry(entry);
127 out.write(wholeFile, 0, fileLength);
128 out.closeEntry();
129 }
130 }