69 | 69 |
final ArrayList<ZipEntry> sortedList = new ArrayList<ZipEntry>();
|
70 | 70 |
while (e.hasMoreElements()) {
|
71 | 71 |
final ZipEntry entry = e.nextElement();
|
72 | |
sortedList.add(entry);
|
|
72 |
// META-INF/ doesn't need a directory entry
|
|
73 |
if (!"META-INF/".equals(entry.getName())) {
|
|
74 |
sortedList.add(entry);
|
|
75 |
}
|
73 | 76 |
}
|
74 | 77 |
|
75 | 78 |
Collections.sort(sortedList, new Comparator<ZipEntry>()
|
76 | 79 |
{
|
77 | 80 |
public int compare(ZipEntry o1, ZipEntry o2)
|
78 | 81 |
{
|
79 | |
return o1.getName().compareTo(o2.getName());
|
|
82 |
String n1 = o1.getName(), n2 = o2.getName();
|
|
83 |
if (metaOverride(n1, n2)) {
|
|
84 |
return -1;
|
|
85 |
}
|
|
86 |
if (metaOverride(n2, n1)) {
|
|
87 |
return 1;
|
|
88 |
}
|
|
89 |
return n1.compareTo(n2);
|
|
90 |
}
|
|
91 |
|
|
92 |
// make sure that META-INF/MANIFEST.MF is always the very first entry
|
|
93 |
private boolean metaOverride(String n1, String n2) {
|
|
94 |
return (n1.startsWith("META-INF/") && !n2.startsWith("META-INF/"))
|
|
95 |
|| (n1.equals("META-INF/MANIFEST.MF") && !n2.equals(n1));
|
80 | 96 |
}
|
81 | 97 |
});
|
82 | 98 |
|
|
104 | 120 |
isEmptyDirectory = false;
|
105 | 121 |
}
|
106 | 122 |
|
107 | |
|
108 | |
// write the entry
|
109 | 123 |
if (isEmptyDirectory)
|
110 | 124 |
{
|
111 | |
sortedList.remove(inputEntry);
|
|
125 |
sortedList.remove(i);
|
112 | 126 |
}
|
113 | |
else
|
114 | |
{
|
115 | |
final ZipEntry outputEntry = new ZipEntry(inputEntry);
|
116 | |
outputStream.putNextEntry(outputEntry);
|
117 | |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
118 | |
final InputStream is = inputZip.getInputStream(inputEntry);
|
119 | |
IoUtil.pipe(is, baos, buf);
|
120 | |
is.close();
|
121 | |
outputStream.write(baos.toByteArray());
|
122 | |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
// finally write entries in normal order
|
|
130 |
for (int i = 0; i < sortedList.size(); i++)
|
|
131 |
{
|
|
132 |
final ZipEntry inputEntry = sortedList.get(i);
|
|
133 |
final ZipEntry outputEntry = new ZipEntry(inputEntry);
|
|
134 |
outputStream.putNextEntry(outputEntry);
|
|
135 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
136 |
final InputStream is = inputZip.getInputStream(inputEntry);
|
|
137 |
IoUtil.pipe(is, baos, buf);
|
|
138 |
is.close();
|
|
139 |
outputStream.write(baos.toByteArray());
|
123 | 140 |
}
|
124 | 141 |
} finally {
|
125 | 142 |
outputStream.close();
|
|
143 |
inputZip.close();
|
126 | 144 |
}
|
127 | 145 |
|
128 | 146 |
}
|