当我尝试解压缩时,ZipOutPutStream,ZipEntry返回null [英] ZipOutPutStream, ZipEntry return null when I try to dezip

查看:466
本文介绍了当我尝试解压缩时,ZipOutPutStream,ZipEntry返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个压缩文件的功能,还有一个解压缩文件的功能.当我尝试解压缩使用自己的函数生成的归档文件时,会出现空指针异常,因为它未找到ZipEntry ...当我尝试使用dezip函数对用winzip制作的归档文件进行解压缩时,它可以工作

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.

但是,我可以使用Winzip打开和解压缩由程序生成的档案,文件"content"在这里,其内容还可以.仅当我使用解压缩功能尝试时,才会发生该错误!

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 !

这是代码:

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

感谢您的帮助

推荐答案

在通过ZipOutputStream zos

我认为您应该删除行:

output.writeTo(foutput);

这篇关于当我尝试解压缩时,ZipOutPutStream,ZipEntry返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆