gpt4 book ai didi

java - Apache Commons ZipArchiveOutputStream 在添加具有非 ASCII 字符的文件名时中断

转载 作者:行者123 更新时间:2023-11-30 06:37:01 27 4
gpt4 key购买 nike

我正在使用 org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream 添加来自 Subversion 存储库的文件。只要我不在文件名中使用德语元音变音符 (ä,ö,ü) 或任何其他特殊字符,它就可以正常工作。我想知道让它接受非 ASCII 字符的最快方法是什么?

def zip(repo: SVNRepository, out: OutputStream, url: String, resourceList: Seq  
[SVNResource]) {
val zout = new ZipArchiveOutputStream(new BufferedOutputStream(out))
zout.setEncoding("Cp437");
zout.setFallbackToUTF8(true);
zout.setUseLanguageEncodingFlag(true);
zout.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE);
try {
for (resource <- resourceList) {
addFileToStream(repo, zout, resource)
}
}
finally {
zout.finish
zout.close
}
}

private def addFileToStream(repo: SVNRepository, zout: ZipArchiveOutputStream, resource:SVNResource): ZipArchiveOutputStream = {
val entry = resource.entry
val url = YSTRepo.getAbsolutePath(entry)
if (FILE == entry.getKind.toString) {
val file = new File(url)
val zipEntry = new ZipArchiveEntry(file, url)
zout.putArchiveEntry(zipEntry)
val baos = new ByteArrayOutputStream()
val fileprops = new SVNProperties()
repo.getFile(url, -1, fileprops, baos)
IOUtils.copy(new ByteArrayInputStream(baos.toByteArray), zout)
zout.closeArchiveEntry
} else if (DIR == entry.getKind.toString) {
if (resource.hasChildren) {
val dirProps = new SVNProperties()
val entries = repo.getDir(url, -1, dirProps, new java.util.ArrayList[SVNDirEntry])
for (child <- SVNResource.listDir(repo, entries.toList.asInstanceOf[Seq SVNDirEntry]])) {
addFileToStream(repo, zout, child)
}
}
}
zout
}

最佳答案

我通过设置解决了这个问题

UnicodeExtraFieldPolicy.NOT_ENCODEABLE 

UnicodeExtraFieldPolicy.ALWAYS

现在可以使用 Linux-Unzip、Windows-Compressed-Folders、IZArc 和 WINZIP 正确显示文件名。

关于java - Apache Commons ZipArchiveOutputStream 在添加具有非 ASCII 字符的文件名时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4034505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com