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 package org.eclipse.jgit.archive;
44
45 import java.io.IOException;
46 import java.io.OutputStream;
47 import java.text.MessageFormat;
48 import java.util.Arrays;
49 import java.util.Collections;
50 import java.util.List;
51 import java.util.Map;
52
53 import org.apache.commons.compress.archivers.ArchiveOutputStream;
54 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
55 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
56 import org.eclipse.jgit.api.ArchiveCommand;
57 import org.eclipse.jgit.archive.internal.ArchiveText;
58 import org.eclipse.jgit.lib.FileMode;
59 import org.eclipse.jgit.lib.ObjectId;
60 import org.eclipse.jgit.lib.ObjectLoader;
61 import org.eclipse.jgit.revwalk.RevCommit;
62
63
64
65
66 public final class ZipFormat extends BaseFormat implements
67 ArchiveCommand.Format<ArchiveOutputStream> {
68 private static final List<String> SUFFIXES = Collections
69 .unmodifiableList(Arrays.asList(".zip"));
70
71
72 @Override
73 public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
74 throws IOException {
75 return createArchiveOutputStream(s,
76 Collections.<String, Object> emptyMap());
77 }
78
79
80 @Override
81 public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
82 Map<String, Object> o) throws IOException {
83 return applyFormatOptions(new ZipArchiveOutputStream(s), o);
84 }
85
86
87 @Override
88 public void putEntry(ArchiveOutputStream out,
89 ObjectId tree, String path, FileMode mode, ObjectLoader loader)
90 throws IOException {
91
92
93 if (path.endsWith("/") && mode != FileMode.TREE)
94 throw new IllegalArgumentException(MessageFormat.format(
95 ArchiveText.get().pathDoesNotMatchMode, path, mode));
96 if (!path.endsWith("/") && mode == FileMode.TREE)
97 path = path + "/";
98
99 final ZipArchiveEntry entry = new ZipArchiveEntry(path);
100
101 if (tree instanceof RevCommit) {
102 long t = ((RevCommit) tree).getCommitTime() * 1000L;
103 entry.setTime(t);
104 }
105
106 if (mode == FileMode.TREE) {
107 out.putArchiveEntry(entry);
108 out.closeArchiveEntry();
109 return;
110 }
111
112 if (mode == FileMode.REGULAR_FILE) {
113
114 } else if (mode == FileMode.EXECUTABLE_FILE
115 || mode == FileMode.SYMLINK) {
116 entry.setUnixMode(mode.getBits());
117 } else {
118
119 throw new IllegalArgumentException(MessageFormat.format(
120 ArchiveText.get().unsupportedMode, mode));
121 }
122 entry.setSize(loader.getSize());
123 out.putArchiveEntry(entry);
124 loader.copyTo(out);
125 out.closeArchiveEntry();
126 }
127
128
129 @Override
130 public Iterable<String> suffixes() {
131 return SUFFIXES;
132 }
133
134
135 @Override
136 public boolean equals(Object other) {
137 return (other instanceof ZipFormat);
138 }
139
140
141 @Override
142 public int hashCode() {
143 return getClass().hashCode();
144 }
145 }