如何使用 ZipEntry 在 Zip 中添加重复文件
How to add duplicate file in Zip using ZipEntry
我有一个文件列表,该列表可能包含重复的文件名,但这些文件位于不同的位置,具有不同的数据。现在,当我尝试将这些文件添加到 zip 中时,我得到 java.lang.Exception:重复条目:File1.xlsx。请建议我如何添加重复的文件名。一种解决方案就像我可以将重复文件重命名为 File ,File_1,File_2..但我不确定如何实现它。请帮忙 !!!如果所有文件名都是唯一的,下面是我的工作代码。
Resource resource = null;
try (ZipOutputStream zippedOut = new ZipOutputStream(response.getOutputStream())) {
for (String file : fileNames) {
resource = new FileSystemResource(file);
if(!resource.exists() && resource != null) {
ZipEntry e = new ZipEntry(resource.getFilename());
//Configure the zip entry, the properties of the file
e.setSize(resource.contentLength());
e.setTime(System.currentTimeMillis());
// etc.
zippedOut.putNextEntry(e);
//And the content of the resource:
StreamUtils.copy(resource.getInputStream(), zippedOut);
zippedOut.closeEntry();
}
}
//zippedOut.close();
zippedOut.finish();
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=download.zip").body(zippedOut);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
One solution is like if I can rename the duplicate file as File
, File_1
, File_2
, ... But I am not sure how I can achieve it.
构建 Set
个名称,并在需要时附加一个数字以使名称唯一,例如
Set<String> names = new HashSet<>();
for (String file : fileNames) {
// ...
String name = resource.getFilename();
String originalName = name;
for (int i = 1; ! names.add(name); i++)
name = originalName + "_" + i;
ZipEntry e = new ZipEntry(name);
// ...
}
代码依赖于 add()
如果名称已经在 Set
中,即返回 false
,即如果名称重复。
即使给定的名字已经编号,这仍然有效,例如这是给定传入名称顺序的映射名称示例:
foo_2
foo
foo -> foo_1
foo -> foo_3 foo_2 was skipped
foo -> foo_4
foo_1 -> foo_1_1 number appended to make unique
我有一个文件列表,该列表可能包含重复的文件名,但这些文件位于不同的位置,具有不同的数据。现在,当我尝试将这些文件添加到 zip 中时,我得到 java.lang.Exception:重复条目:File1.xlsx。请建议我如何添加重复的文件名。一种解决方案就像我可以将重复文件重命名为 File ,File_1,File_2..但我不确定如何实现它。请帮忙 !!!如果所有文件名都是唯一的,下面是我的工作代码。
Resource resource = null;
try (ZipOutputStream zippedOut = new ZipOutputStream(response.getOutputStream())) {
for (String file : fileNames) {
resource = new FileSystemResource(file);
if(!resource.exists() && resource != null) {
ZipEntry e = new ZipEntry(resource.getFilename());
//Configure the zip entry, the properties of the file
e.setSize(resource.contentLength());
e.setTime(System.currentTimeMillis());
// etc.
zippedOut.putNextEntry(e);
//And the content of the resource:
StreamUtils.copy(resource.getInputStream(), zippedOut);
zippedOut.closeEntry();
}
}
//zippedOut.close();
zippedOut.finish();
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=download.zip").body(zippedOut);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
One solution is like if I can rename the duplicate file as
File
,File_1
,File_2
, ... But I am not sure how I can achieve it.
构建 Set
个名称,并在需要时附加一个数字以使名称唯一,例如
Set<String> names = new HashSet<>();
for (String file : fileNames) {
// ...
String name = resource.getFilename();
String originalName = name;
for (int i = 1; ! names.add(name); i++)
name = originalName + "_" + i;
ZipEntry e = new ZipEntry(name);
// ...
}
代码依赖于 add()
如果名称已经在 Set
中,即返回 false
,即如果名称重复。
即使给定的名字已经编号,这仍然有效,例如这是给定传入名称顺序的映射名称示例:
foo_2
foo
foo -> foo_1
foo -> foo_3 foo_2 was skipped
foo -> foo_4
foo_1 -> foo_1_1 number appended to make unique