Java 解压和压缩文件(文件夹)
2012-12-15 16:23
555 查看
package notion.touch.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import notion.basic.vo.exception.BusinessException; /** * 压缩文件夹公共类 * @author ganxz * @date 2012-12-19 * */ public class ZipUtils { /** * 文件夹压缩公共方法 * @param inputdir 要压缩的文件夹路径 * @param zipFileName 要导出压缩文件的路径 * @throws Exception */ public static void zipFolder(String inputdir,String zipFileName) throws Exception { zipFileName = zipFileName.replace("/", "\\"); String zipdir = zipFileName.substring(0,zipFileName.lastIndexOf("\\")); File zipDirFile = new File(zipdir); if(!zipDirFile.exists()) zipDirFile.mkdirs(); zip(zipFileName, new File(inputdir)); } private static void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); zip(out, inputFile, ""); System.out.println("压缩完成"); out.close(); } private static void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { File[] fl = f.listFiles(); out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } }else { out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; System.out.println(base); while ( (b = in.read()) != -1) { out.write(b); } in.close(); } } /** * 压缩文件 * @param filepath 要压缩的文件路径 * @param zipname 要导出的压缩文件路径 * @throws Exception */ public static void zipFile(String filepath,String zipname) throws Exception{ File f = new File(filepath); if(f.exists()) throw new Exception("要压缩的文件不存在,请检查!"); FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis); byte[] buf = new byte[1024]; int len; if(zipname == null || zipname == "" ){ zipname = f.getName()+".zip"; }else{ if(!zipname.substring(zipname.indexOf(".")).equals(".zip")) zipname += ".zip"; } FileOutputStream fos = new FileOutputStream(zipname); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos);//压缩包 ZipEntry ze = new ZipEntry(f.getName());//这是压缩包名里的文件名 zos.putNextEntry(ze);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处 while((len=bis.read(buf))!=-1) { zos.write(buf,0,len); zos.flush(); } bis.close(); zos.close(); } /** * 解压 压缩文件 * @param zipFileName 压缩文件的路径 * @param extPlace 解压文件夹路径 * @throws Exception */ public static void extZipFileList(String zipFileName, String extPlace) throws Exception { try { ZipInputStream in = new ZipInputStream(new FileInputStream( zipFileName)); ZipEntry entry = null; while ((entry = in.getNextEntry()) != null) { String entryName = entry.getName(); String dir = extPlace + entryName; File f = new File(dir.substring(0,dir.lastIndexOf("/"))); if(!f.exists()) f.mkdirs(); if (entry.isDirectory()) { File file = new File(extPlace + entryName); file.mkdirs(); } else { FileOutputStream os = new FileOutputStream(extPlace + entryName); // Transfer bytes from the ZIP file to the output file byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { os.write(buf, 0, len); } os.close(); in.closeEntry(); } } } catch (Exception e) { throw new Exception("解压文件失败!原因:",e); } } }
相关文章推荐
- 利用java的ZipOutputStream类压缩文件,解压文件或文件夹中的文件
- java 压缩、解压文件、文件夹。
- java 实现对文件文件夹压缩、解压
- Java压缩解压文件/文件夹
- Java 文件或者文件夹的压缩和解压
- java压缩及解压(.zip)文件文件夹
- 使用Java对文件或文件夹的压缩, 解压, 加密和解密
- -Java压缩解压文件、文件夹的一个工具类
- java 压缩/解压 zip 多个文件和文件夹
- Java文件的压缩和解压操作
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- java压缩和解压文件(支持中文文件名)
- java实现zip压缩文件/文件夹
- java Gzip方式 解压,压缩文件Utils
- Java(Android)文件压缩和解压
- JAVA压缩文件、解压文件、删除文件
- 压缩打包linux 文件压缩解压命令Strut2教程-java教程
- 【转载】用java压缩文件夹/文件
- java 压缩解压文件 无乱码
- Java解压、压缩文件