首页 > 代码库 > java解压缩一个文件

java解压缩一个文件

/** * 解压缩一个文件 * * @param zipFile * 压缩文件 * @param folderPath * 解压缩的目标目录 * @throws IOException * 当解压缩过程出错时抛出 */ public static void unZipFile(File zipFile, String folderPath) throws ZipException, IOException { String sub = zipFile.getName(); int pos = sub.lastIndexOf("."); if (pos >= 0) { sub = sub.substring(0, pos); } File subDir = new File(folderPath + File.separator + sub); if (subDir.exists()) { deleteFile(subDir); } subDir.mkdirs(); ZipFile zf = new ZipFile(zipFile); for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) { ZipEntry entry = ((ZipEntry) entries.nextElement()); InputStream in = zf.getInputStream(entry); String str = subDir.getAbsolutePath() + File.separator + entry.getName(); // str = new String(str.getBytes("8859_1"), "utf-8"); File desFile = new File(str); if (!desFile.exists()) { File fileParentDir = desFile.getParentFile(); if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } if (entry.isDirectory()) { desFile.mkdirs(); continue; } desFile.createNewFile(); } OutputStream out = new FileOutputStream(desFile); byte buffer[] = new byte[10240]; int realLength; while ((realLength = in.read(buffer)) > 0) { out.write(buffer, 0, realLength); } in.close(); out.close(); } zf.close(); } private static void deleteFile(File file) { if (file.isDirectory()) { File[] fs = file.listFiles(); if (fs != null) { for (File f : fs) { deleteFile(f); } } } file.delete(); }

java解压缩一个文件