ZipOutPutStream, ZipEntry return null when I try t

2019-08-09 04:46发布

问题:

I have a function to compress a file and another to decompress it. When I try to decompress an archive I've generated with my own function, there is a null pointer exception because it doesn't found the ZipEntry... When I try to decompress an archive made with winzip with my dezip function, it works.

However I can open and decompress an archive generated by my program with winzip, the file "content" is here and its content is ok. The error occur only when I try it with my dezip function !

Here's the code :

public static void zip() {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();

            /* Code to create the xml */

            Properties outFormat = new Properties();
            /* properties */

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperties(outFormat);

            ByteArrayOutputStream output = new ByteArrayOutputStream();
            File file = new File("KOKO.zip");
            FileOutputStream foutput = new FileOutputStream(file);

            DOMSource domSource = new DOMSource(document.getDocumentElement());
            StreamResult result = new StreamResult(output);
            transformer.transform(domSource, result);
            output.writeTo(foutput);

            ZipOutputStream zos = new ZipOutputStream(foutput);
            byte[] bytes = output.toByteArray();
            ZipEntry entry = new ZipEntry("content");
            zos.putNextEntry(entry);
            zos.write(bytes);
            zos.closeEntry();
            zos.close();

       /* here the catch clauses */
    }

    public static void unzip(File zipfile, File folder) throws FileNotFoundException, IOException {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
        ZipEntry ze = null;
        try {
            ze = zis.getNextEntry();
            System.out.println("path :"+ zipfile.getAbsolutePath());
            File f = new File(folder.getAbsolutePath(), ze.getName());
            f.getParentFile().mkdirs();
            OutputStream fos = new BufferedOutputStream(new FileOutputStream(f));
            try {
                try {
                    final byte[] buf = new byte[8192];
                    int bytesRead;
                    while (-1 != (bytesRead = zis.read(buf)))
                        fos.write(buf, 0, bytesRead);
                } finally {
                    fos.close();
                }
            } catch (final IOException ioe) {
                f.delete();
                throw ioe;
            }
        } finally {
            zis.close();
        }
    }

Thanks for your help

回答1:

You are writing to your zip file foutput before you start writing to it through ZipOutputStream zos

I think you should remove line :

output.writeTo(foutput);