JAVA解压Zip格式文件的代码

代码如下,已经测试:

    private static void processZipFile(final String dir, final File zf) throws IOException
    {
        ZipFile zipFile = new ZipFile(zf.toString());
        Enumeration entries = zipFile.entries(); 
        while (entries.hasMoreElements()) 
        { 
            ZipEntry entry = (ZipEntry)entries.nextElement(); 
            String filename = dir + File.separator + entry.getName(); 

            File file = new File(filename); 
            if (entry.isDirectory())
            {
                file.mkdirs(); 
                continue; 
            }
            file.createNewFile();
            
            InputStream      is  = zipFile.getInputStream(entry); 
            FileOutputStream fos = new FileOutputStream(file); 
            int count = 0;
            byte buf[] = new byte[4096]; 
            while ((count = is.read(buf)) > 0) 
            { 
                bos.write(buf, 0, count); 
            }
            fos.close(); 
            is.close(); 
        }
        zipFile.close(); 
    }

 

你可能感兴趣的:(JAVA)