加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

压缩(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)查看您的文件(以及“好”文件),以查看可能出错的内容.

(一种可能性是您使用了错误的压缩算法.还有其他几种变体需要考虑,特别是您是否生成“目录”.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读