The compressed (zipped) folder is not a valid Java folder

I'm trying to use zipoutputstream to compress files from the server into a folder

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();

Solution

Zip can open a variety of zipper formats, and relatively tolerate strange Windows double click requires a relatively specific format and much lower tolerance

You need to find the ZIP format, and then use a hex editor (such as Hex Editor Neo) to view your file (and the "good" file) to see what might go wrong

(one possibility is that you use the wrong compression algorithm. There are several other variants to consider, especially whether you generate a "directory")

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>