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.ObjectLoader;
60
61
62
63
64 public final class ZipFormat extends BaseFormat implements
65 ArchiveCommand.Format<ArchiveOutputStream> {
66 private static final List<String> SUFFIXES = Collections
67 .unmodifiableList(Arrays.asList(".zip"));
68
69 public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
70 throws IOException {
71 return createArchiveOutputStream(s,
72 Collections.<String, Object> emptyMap());
73 }
74
75
76
77
78 public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
79 Map<String, Object> o) throws IOException {
80 return applyFormatOptions(new ZipArchiveOutputStream(s), o);
81 }
82
83 public void putEntry(ArchiveOutputStream out,
84 String path, FileMode mode, ObjectLoader loader)
85 throws IOException {
86
87
88 if (path.endsWith("/") && mode != FileMode.TREE)
89 throw new IllegalArgumentException(MessageFormat.format(
90 ArchiveText.get().pathDoesNotMatchMode, path, mode));
91 if (!path.endsWith("/") && mode == FileMode.TREE)
92 path = path + "/";
93
94 final ZipArchiveEntry entry = new ZipArchiveEntry(path);
95 if (mode == FileMode.TREE) {
96 out.putArchiveEntry(entry);
97 out.closeArchiveEntry();
98 return;
99 }
100
101 if (mode == FileMode.REGULAR_FILE) {
102
103 } else if (mode == FileMode.EXECUTABLE_FILE
104 || mode == FileMode.SYMLINK) {
105 entry.setUnixMode(mode.getBits());
106 } else {
107
108 throw new IllegalArgumentException(MessageFormat.format(
109 ArchiveText.get().unsupportedMode, mode));
110 }
111 entry.setSize(loader.getSize());
112 out.putArchiveEntry(entry);
113 loader.copyTo(out);
114 out.closeArchiveEntry();
115 }
116
117 public Iterable<String> suffixes() {
118 return SUFFIXES;
119 }
120
121 @Override
122 public boolean equals(Object other) {
123 return (other instanceof ZipFormat);
124 }
125
126 @Override
127 public int hashCode() {
128 return getClass().hashCode();
129 }
130 }