码迷,mamicode.com
首页 > 编程语言 > 详细

java解压缩一个文件

时间:2015-01-06 20:10:32      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:java   android   解压缩   

/** * 解压缩一个文件 * * @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解压缩一个文件

标签:java   android   解压缩   

原文地址:http://blog.csdn.net/u013626215/article/details/42462459

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!