Skip to content

Commit 7a0a8a0

Browse files
committed
Allow export of multiple zip entries with the same name
1 parent 1d3e555 commit 7a0a8a0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/main/java/me/coley/recaf/command/impl/Export.java

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.coley.recaf.plugin.PluginsManager;
55
import me.coley.recaf.plugin.api.ExportInterceptorPlugin;
66
import me.coley.recaf.util.IOUtil;
7+
import me.coley.recaf.util.Log;
78
import me.coley.recaf.workspace.ClassResource;
89
import me.coley.recaf.workspace.DirectoryResource;
910
import me.coley.recaf.workspace.JavaResource;
@@ -16,6 +17,7 @@
1617
import java.io.File;
1718
import java.io.IOException;
1819
import java.io.OutputStream;
20+
import java.lang.reflect.Field;
1921
import java.nio.file.Files;
2022
import java.nio.file.Path;
2123
import java.nio.file.Paths;
@@ -141,6 +143,13 @@ public static void writeArchive(boolean compress, File output, Map<String, byte[
141143
OutputStream os = new BufferedOutputStream(Files.newOutputStream(output.toPath()), 1048576);
142144
try (ZipOutputStream jos = ("zip".equals(extension)) ? new ZipOutputStream(os) :
143145
/* Let's assume it's a jar */ new JarOutputStream(os)) {
146+
try {
147+
Field field = ZipOutputStream.class.getDeclaredField("names");
148+
field.setAccessible(true);
149+
field.set(jos, new DiscardingSet());
150+
} catch (NoSuchFieldException | IllegalAccessException ex) {
151+
Log.error(ex, "Could not replace ZIP names");
152+
}
144153
PluginsManager pluginsManager = PluginsManager.getInstance();
145154
Set<String> dirsVisited = new HashSet<>();
146155
// Contents is iterated in sorted order (because 'archiveContent' is TreeMap).
@@ -197,4 +206,20 @@ private void put(Map<String, byte[]> content, JavaResource res) {
197206
content.put(name, e.getValue());
198207
}
199208
}
209+
210+
/**
211+
* A set that discards it's elements upon adding.
212+
*
213+
* This class is used to prevent "Duplcicate zip entry: "
214+
* error
215+
*
216+
* @author xDark
217+
*/
218+
private static final class DiscardingSet extends HashSet<String> {
219+
220+
@Override
221+
public boolean add(String s) {
222+
return true;
223+
}
224+
}
200225
}

0 commit comments

Comments
 (0)