压缩(zipped)文件夹是无效的Java
发布时间:2020-12-15 08:46:14 所属栏目:Java 来源:网络整理
导读:我正在尝试使用ZipOutputStream将文件从服务器压缩到一个文件夹中. 存档下载后,双击后无法打开.错误“压缩(压缩)文件夹无效”发生.但是,如果我从上下文菜单中打开它 – 7zip – 打开文件它正常工作.这个问题可能是什么原因? sourceFileName="./file.txt"' s
我正在尝试使用ZipOutputStream将文件从服务器压缩到一个文件夹中.
存档下载后,双击后无法打开.错误“压缩(压缩)文件夹无效”发生.但是,如果我从上下文菜单中打开它 – > 7zip – >打开文件它正常工作.这个问题可能是什么原因? sourceFileName="./file.txt"' sourceFile = new File(sourceFileName); try { // set the content type and the filename responce.setContentType("application/zip"); response.addHeader("Content-Disposition","attachment; filename=" + sourceFileName + ".zip"); responce.setContentLength((int) sourceFile.length()); // get a ZipOutputStream,so we can zip our files together ZipOutputStream outZip = new ZipOutputStream((responce.getOutputStream()); // Add ZIP entry to output stream. outZip.putNextEntry(new ZipEntry(sourceFile.getName())); int length = 0; byte[] bbuf = new byte[(int) sourceFile.length()]; DataInputStream in = new DataInputStream(new FileInputStream(sourceFile)); while ((in != null) && ((length = in.read(bbuf)) != -1)) { outZip.write(bbuf,length); } outZip.closeEntry(); in.close(); outZip.flush(); outZip.close(); 解决方法
7Zip可以打开各种各样的拉链格式,并且相对容忍奇怪. Windows双击需要相对特定的格式,并且容忍度要低得多.
您需要查找zip format,然后使用十六进制编辑器(例如Hex Editor Neo)查看您的文件(以及“好”文件),以查看可能出错的内容. (一种可能性是您使用了错误的压缩算法.还有其他几种变体需要考虑,特别是您是否生成“目录”.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- java – JPA多对多连接表实体,复合键“null id g
- java – 文档获取/更新/删除时Lucene索引文件的更
- java – setSpan的IndexOutOfBoundsException –
- [JSOI2009]密码——AC自动机+记忆化搜索(状压)
- java – Eclipse:OSGI首选项与PreferenceStore
- java – 为什么时区模式“OOOO”没有显示完整的
- java实现快速排序算法
- java – Apache Commons数学优化
- 什么Java数据类型对应于Oracle SQL数据类型NUMER
- java – Spring安全和自定义AuthenticationFilte
热点阅读