本帖最后由 Barnes 于 2022-8-5 20:25 编辑
把压缩文件中的每一个文件,名字最后都加个"/",就会被解压缩软件认作目录了。把"/"删掉还可以恢复,挺有意思的
[Java] 纯文本查看 复制代码 public static void main(String[] args) throws Exception {
ZipFile originalFile = new ZipFile(BASE_DIR + "1.zip");
ZipOutputStream fixedFile = new ZipOutputStream(new FileOutputStream(BASE_DIR + "Fixed.zip"));
Enumeration<? extends ZipEntry> entries = originalFile.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
byte[] bytes = toByteArray(originalFile.getInputStream(entry));
// 文件夹bytes == 0
fixedFile.putNextEntry(new ZipEntry(entry.getName().substring(0, entry.getName().length() - (bytes.length == 0 ? 0 : 1))));
fixedFile.write(bytes, 0, bytes.length);
fixedFile.closeEntry();
}
fixedFile.close();
originalFile.close();
}
private static byte[] toByteArray(InputStream inputStream) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int temp = 0;
while((temp = inputStream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, temp);
}
return out.toByteArray();
} |