- public static void unzipFile(File zip, File dir) throws IOException {
- ZipFile zipFile = new ZipFile(zip.getPath());
- Enumeration e = zipFile.getEntries();
- ZipEntry zipEntry = null;
- List folderList = new ArrayList();
- List fileList = new ArrayList();
- while (e.hasMoreElements())
- {
- zipEntry = (ZipEntry) e.nextElement();
- if (zipEntry.isDirectory())
- {
- folderList.add(zipEntry);
- }
- else if (!zipEntry.isDirectory())
- {
- fileList.add(zipEntry);
- }
- }
- for (int i = 0; i < folderList.size(); i++)
- {
- zipEntry = (ZipEntry) folderList.get(i);
- String name = zipEntry.getName();
- File f = new File(dir, name);
- name = name.substring(0, name.length() - 1);
- f.mkdirs();
- }
- for (int j = 0; j < fileList.size(); j++)
- {
- zipEntry = (ZipEntry) fileList.get(j);
- String name = zipEntry.getName();
- File f = new File(dir, name);
- if (!f.exists())
- {
- f.createNewFile();
- InputStream in = zipFile.getInputStream(zipEntry);
- FileOutputStream out = new FileOutputStream(f);
- int c;
- byte[] by = new byte[1024];
- while ((c = in.read(by)) != -1)
- {
- out.write(by, 0, c);
- }
- out.close();
- in.close();
- }
- }
- zipFile.close();
- }
java的解压压缩文件的程序
最新推荐文章于 2023-06-13 10:20:37 发布