使用http://www.java2s.com/Code/Java/File-Input-Output/Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm稍微修改一下,将目录压缩到一个 zip 中,我剩下这个:
源路径
E:someDir/someDir/somefile
和 .zip 中的路径
E:someDir/someDir/somefile
我想进入 .zip 是
someDir/somefile
但是,如果程序的用户之间的完整目录路径会有所不同,如何实现这一点?
最佳答案
根据您帖子的示例,这一行是需要修改的内容:
out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
应该是
out.putNextEntry(new ZipEntry("someDir/somefile"));
但是你应该能够从完整的源路径E:someDir/someDir/somefile
派生someDir/somefile
。您应该能够通过子字符串或附加 File.getParentFile() 来完成此操作
关于Java删除部分文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162685/