apache.commons.compress zip压缩解压工具类完整版(使用递归支持无限文件夹的解压缩)
2015-03-13 11:12
369 查看
压缩解压工具类完整版(使用递归支持无限文件夹的解压缩)
网上一致没有找到可以完全无限文件夹的解压缩,并支持中文的zip工具类,所以自己写了一个在这里分享一下。
说明:jdk自带的zip中文会乱码,所以这里采用的是apache 的compress
注意:需要导入jar :commons-compress-1.9.jar
网上一致没有找到可以完全无限文件夹的解压缩,并支持中文的zip工具类,所以自己写了一个在这里分享一下。
说明:jdk自带的zip中文会乱码,所以这里采用的是apache 的compress
注意:需要导入jar :commons-compress-1.9.jar
<pre name="code" class="java">package test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.Zip64Mode; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; public class ZipUtil { public static void main(String[] args) { String dir = "D:\\workspace\\runtime-com.qz.meta.product\\xxxx\\.image"; String zippath = "D:\\test2.zip"; ZipUtil.zip(dir, zippath); String unzipdir = "D:\\test2"; String unzipfile = "D:\\test2.zip"; ZipUtil.unzip(unzipfile, unzipdir); System.out.println("success!"); } /** * zip压缩文件 * @param dir * @param zippath */ public static void zip(String dir ,String zippath){ List<String> paths = getFiles(dir); compressFilesZip(paths.toArray(new String[paths.size()]),zippath,dir ); } /** * 递归取到当前目录所有文件 * @param dir * @return */ public static List<String> getFiles(String dir){ List<String> lstFiles = null; if(lstFiles == null){ lstFiles = new ArrayList<String>(); } File file = new File(dir); File [] files = file.listFiles(); for(File f : files){ if(f.isDirectory()){ lstFiles.add(f.getAbsolutePath()); lstFiles.addAll(getFiles(f.getAbsolutePath())); }else{ String str =f.getAbsolutePath(); lstFiles.add(str); } } return lstFiles; } /** * 文件名处理 * @param dir * @param path * @return */ public static String getFilePathName(String dir,String path){ String p = path.replace(dir+File.separator, ""); p = p.replace("\\", "/"); return p; } /** * 把文件压缩成zip格式 * @param files 需要压缩的文件 * @param zipFilePath 压缩后的zip文件路径 ,如"D:/test/aa.zip"; */ public static void compressFilesZip(String[] files,String zipFilePath,String dir) { if(files == null || files.length <= 0) { return ; } ZipArchiveOutputStream zaos = null; try { File zipFile = new File(zipFilePath); zaos = new ZipArchiveOutputStream(zipFile); zaos.setUseZip64(Zip64Mode.AsNeeded); //将每个文件用ZipArchiveEntry封装 //再用ZipArchiveOutputStream写到压缩文件中 for(String strfile : files) { File file = new File(strfile); if(file != null) { String name = getFilePathName(dir,strfile); ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file,name); zaos.putArchiveEntry(zipArchiveEntry); if(file.isDirectory()){ continue; } InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024 ]; int len = -1; while((len = is.read(buffer)) != -1) { //把缓冲区的字节写入到ZipArchiveEntry zaos.write(buffer, 0, len); } zaos.closeArchiveEntry(); }catch(Exception e) { throw new RuntimeException(e); }finally { if(is != null) is.close(); } } } zaos.finish(); }catch(Exception e){ throw new RuntimeException(e); }finally { try { if(zaos != null) { zaos.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } /** * 把zip文件解压到指定的文件夹 * @param zipFilePath zip文件路径, 如 "D:/test/aa.zip" * @param saveFileDir 解压后的文件存放路径, 如"D:/test/" () */ public static void unzip(String zipFilePath, String saveFileDir) { if(!saveFileDir.endsWith("\\") && !saveFileDir.endsWith("/") ){ saveFileDir += File.separator; } File dir = new File(saveFileDir); if(!dir.exists()){ dir.mkdirs(); } File file = new File(zipFilePath); if (file.exists()) { InputStream is = null; ZipArchiveInputStream zais = null; try { is = new FileInputStream(file); zais = new ZipArchiveInputStream(is); ArchiveEntry archiveEntry = null; while ((archiveEntry = zais.getNextEntry()) != null) { // 获取文件名 String entryFileName = archiveEntry.getName(); // 构造解压出来的文件存放路径 String entryFilePath = saveFileDir + entryFileName; OutputStream os = null; try { // 把解压出来的文件写到指定路径 File entryFile = new File(entryFilePath); if(entryFileName.endsWith("/")){ entryFile.mkdirs(); }else{ os = new BufferedOutputStream(new FileOutputStream( entryFile)); byte[] buffer = new byte[1024 ]; int len = -1; while((len = zais.read(buffer)) != -1) { os.write(buffer, 0, len); } } } catch (IOException e) { throw new IOException(e); } finally { if (os != null) { os.flush(); os.close(); } } } } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (zais != null) { zais.close(); } if (is != null) { is.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } } }
相关文章推荐
- 【原创】无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏
- 有关.Net中使用压缩解压缩的问题补充(递归压缩文件夹)
- ZIP解压缩文件的工具类【支持多级文件夹|全】
- 无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏
- 支持递归压缩文件夹-org.apache.tools.zip.ZipEntry
- 递归遍历一个文件夹,对文件进行操作,使用lstat时的悲剧
- sharpziplib的压缩,支持文件,文件夹的递归。
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- java压缩与解压缩(2)使用org.apache.tools.zip
- 使用org.apache.tools.zip包来压缩解压缩文件。
- 补充 ext treepanel 支持无限级 递归
- 采用org.apache.tools.zip来进行zip包的解压缩,支持目录嵌套和中文名
- 使用递归方式删除文件夹内容中的文件
- 使用C#递归删除一个文件夹
- Java使用递归遍历文件夹
- Java使用递归遍历文件夹
- 使用递归方式遍历指定磁盘路径下的文件及文件夹
- linux下压缩与解压缩的简单记忆方法;递归删除文件夹
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持