- 浏览: 52016 次
- 性别:
- 来自: 江西
最新评论
-
czb6788782:
楼主,源码能不能给我学习下~谢谢
Java实现一个压缩小软件 -
hpuyancy:
好像中文会乱码啊?
利用apache ant 包进行压缩、解压缩zip,归档tar,解档tar,压缩tar.gz解压tar.gz -
shutingwang:
不错
Apache POI SpreadSheet的一些简单应用(二) -
gaowei52306:
gaowei52306 写道为啥我下载后双击无法执行?
抱歉, ...
Java实现一个压缩小软件 -
gaowei52306:
为啥我下载后双击无法执行?
Java实现一个压缩小软件
最近用到了利用java进行一序列压缩解压缩,jdk也自带了,这里我就不用它了。本例用到的开源包是apahce ant.jar。我上传了。希望对大家有帮组。
另利用这些,制作了一个java工具实现压缩(zip,tar,tar.gz,tar.bz2)功能.地址是http://lhxctc.iteye.com/blog/709492
引用
Java压缩zip,解压缩zip
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.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; /** * 利用Apache ant.jar中的zip包进行Zip压缩和解压 */ public class XZouZip { /** * 测试压缩 */ public void testZip(){ File srcFile = new File("c:/upload");//要压缩的文件对象 File targetZipFile = new File("c:/upload.zip");//压缩后的文件名 ZipOutputStream out = null; boolean boo = false;//是否压缩成功 try{ CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( targetZipFile), new CRC32()); out = new ZipOutputStream(cos); //out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("c:/uploadd.zip"))); zip(srcFile, out, "", true); boo = true; }catch(IOException ex){ throw new RuntimeException(ex); }finally{ try{ if(out!=null) out.close(); }catch(IOException ex){ throw new RuntimeException("关闭Zip输出流出现异常",ex); }finally{ //清理操作 if(!boo && targetZipFile.exists())//压缩不成功, targetZipFile.delete(); } } } /** * 测试解压缩 */ public void testUnZip(){ File srcZipFile = new File("c:/upload.zip");//要解压缩的zip文件对象 String destDir = "c:/a";//将解压文件对象放置a目录中 boolean boo = false;//是否压缩成功 try { unZip(srcZipFile,destDir); boo = true; } catch (IOException e) { throw new RuntimeException(e); }finally{ //清理操作 if(!boo) deleteDirectory(new File(destDir));//目标文件夹 。清理 } } public static void main(String[] args) throws IOException { XZouZip tool = new XZouZip(); tool.testZip(); //tool.testUnZip(); } /** * 压缩zip文件 * @param file 压缩的文件对象 * @param out 输出ZIP流 * @param dir 相对父目录名称 * @param boo 是否把空目录压缩进去 */ public void zip(File file,ZipOutputStream out,String dir,boolean boo) throws IOException{ if(file.isDirectory()){//是目录 File []listFile = file.listFiles();//得出目录下所有的文件对象 if(listFile.length == 0 && boo){//空目录压缩 out.putNextEntry(new ZipEntry(dir + file.getName() + "/"));//将实体放入输出ZIP流中 System.out.println("压缩." + dir + file.getName() + "/"); return; }else{ for(File cfile: listFile){ zip(cfile,out,dir + file.getName() + "/",boo);//递归压缩 } } }else if(file.isFile()){//是文件 System.out.println("压缩." + dir + file.getName() + "/"); byte[] bt = new byte[2048*2]; ZipEntry ze = new ZipEntry(dir+file.getName());//构建压缩实体 //设置压缩前的文件大小 ze.setSize(file.length()); out.putNextEntry(ze);////将实体放入输出ZIP流中 FileInputStream fis = null; try{ fis = new FileInputStream(file); int i=0; while((i = fis.read(bt)) != -1) {//循环读出并写入输出Zip流中 out.write(bt, 0, i); } }catch(IOException ex){ throw new IOException("写入压缩文件出现异常",ex); }finally{ try{ if (fis != null) fis.close();//关闭输入流 }catch(IOException ex){ throw new IOException("关闭输入流出现异常"); } } } } /** * 解压缩zipFile * @param file 要解压的zip文件对象 * @param outputDir 要解压到某个指定的目录下 * @throws IOException */ public void unZip(File file,String outputDir) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(file); createDirectory(outputDir,null);//创建输出目录 Enumeration<?> enums = zipFile.getEntries(); while(enums.hasMoreElements()){ ZipEntry entry = (ZipEntry) enums.nextElement(); System.out.println("解压." + entry.getName()); if(entry.isDirectory()){//是目录 createDirectory(outputDir,entry.getName());//创建空目录 }else{//是文件 File tmpFile = new File(outputDir + "/" + entry.getName()); createDirectory(tmpFile.getParent() + "/",null);//创建输出目录 InputStream in = null; OutputStream out = null; try{ in = zipFile.getInputStream(entry);; out = new FileOutputStream(tmpFile); int length = 0; byte[] b = new byte[2048]; while((length = in.read(b)) != -1){ out.write(b, 0, length); } }catch(IOException ex){ throw ex; }finally{ if(in!=null) in.close(); if(out!=null) out.close(); } } } } catch (IOException e) { throw new IOException("解压缩文件出现异常",e); } finally{ try{ if(zipFile != null){ zipFile.close(); } }catch(IOException ex){ throw new IOException("关闭zipFile出现异常",ex); } } } /** * 构建目录 * @param outputDir * @param subDir */ public void createDirectory(String outputDir,String subDir){ File file = new File(outputDir); if(!(subDir == null || subDir.trim().equals(""))){//子目录不为空 file = new File(outputDir + "/" + subDir); } if(!file.exists()){ file.mkdirs(); } } /** * 清理文件(目录或文件) * @param file */ public void deleteDirectory(File file){ if(file.isFile()){ file.delete();//清理文件 }else{ File list[] = file.listFiles(); if(list!=null){ for(File f: list){ deleteDirectory(f); } file.delete();//清理目录 } } } }
引用
Java压缩解压缩zip利用ant.更简单
import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Expand; import org.apache.tools.ant.taskdefs.Zip; import org.apache.tools.ant.types.FileSet; /** * 利用Apache ant.jar中的ant包进行Zip压缩和解压 * 这个更为简单 */ public class XZouZipByAnt { public static void main(String[] args) { XZouZipByAnt jzb = new XZouZipByAnt(); jzb.zip("c:/upload", "c:/upload.zip"); //jzb.unZip("c:/a", "c:/upload.zip"); } /** * 解压缩 * @param destDir 生成的目标目录下 c:/a * @param sourceZip 源zip文件 c:/upload.zip * 结果则是 将upload.zip文件解压缩到c:/a目录下 */ public void unZip(String destDir,String sourceZip){ try { Project prj1 = new Project(); Expand expand = new Expand(); expand.setProject(prj1); expand.setSrc(new File(sourceZip)); expand.setOverwrite(false);//是否覆盖 File f = new File(destDir); expand.setDest(f); expand.execute(); } catch (Exception e) { e.printStackTrace(); } } /** * 压缩 * * @param sourceFile * 压缩的源文件 如: c:/upload * @param targetZip * 生成的目标文件 如:c:/upload.zip */ public void zip(String sourceFile,String targetZip){ Project prj = new Project(); Zip zip = new Zip(); zip.setProject(prj); zip.setDestFile(new File(targetZip));//设置生成的目标zip文件File对象 FileSet fileSet = new FileSet(); fileSet.setProject(prj); fileSet.setDir(new File(sourceFile));//设置将要进行压缩的源文件File对象 //fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹,只压缩目录中的所有java文件 //fileSet.setExcludes("**/*.java"); //排除哪些文件或文件夹,压缩所有的文件,排除java文件 zip.addFileset(fileSet); zip.execute(); } }
引用
通过 Apache Tool 进行JAVA tar || tar.gz
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.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarInputStream; import org.apache.tools.tar.TarOutputStream; /** * 通过 Apache Tool 进行JAVA tar || tar.gz */ public class XZouTarAndGz { /** * 测试归档tar文件 */ public File testTar(){ File srcFile = new File("c:/upload");//要归档的文件对象 File targetTarFile = new File("c:/upload.tar");//归档后的文件名 TarOutputStream out = null; boolean boo = false;//是否压缩成功 try{ out = new TarOutputStream(new BufferedOutputStream(new FileOutputStream(targetTarFile))); tar(srcFile, out, "", true); boo = true; //归档成功 return targetTarFile; }catch(IOException ex){ throw new RuntimeException(ex); }finally{ try{ if(out!=null) out.close(); }catch(IOException ex){ throw new RuntimeException("关闭Tar输出流出现异常",ex); }finally{ //清理操作 if(!boo && targetTarFile.exists())//归档不成功, targetTarFile.delete(); } } } /** * 测试压缩归档tar.gz文件 */ public void testTarGz(){ File tarFile = testTar();//生成的tar文件 File gzFile = new File(tarFile + ".gz");//将要生成的压缩文件 GZIPOutputStream out = null; InputStream in = null; boolean boo = false;//是否成功 try{ in = new FileInputStream(tarFile); out = new GZIPOutputStream(new FileOutputStream(gzFile),1024 * 2); byte b[] = new byte[1024 * 2]; int length = 0; while( (length = in.read(b)) != -1 ){ out.write(b,0,length); } boo = true; }catch(Exception ex){ throw new RuntimeException("压缩归档文件失败",ex); }finally{ try{ if(out!=null) out.close(); if(in!=null) in.close(); }catch(IOException ex){ throw new RuntimeException("关闭流出现异常",ex); }finally{ if(!boo){//清理操作 tarFile.delete(); if(gzFile.exists()) gzFile.delete(); } } } } /** * 测试解压归档tar文件 */ public void testUnTar(){ File srcTarFile = new File("c:/upload.tar");//要解压缩的tar文件对象 String destDir = "c:/XZou";//把解压的文件放置到c盘下的XZou目录下面 boolean boo = false;//是否压缩成功 try { unTar(srcTarFile,destDir); boo = true; } catch (IOException e) { throw new RuntimeException(e); }finally{ //清理操作 if(!boo) deleteDirectory(new File(destDir));//目标文件夹 。清理 } } /** * 测试解压归档tar文件 */ public void testUnTarGz(){ File srcTarGzFile = new File("c:/up.tar.gz");//要解压缩的tar.gz文件对象 String destDir = "c:/XZou";//把解压的文件放置到c盘下的XZou目录下面 boolean boo = false;//是否压缩成功 try { unTarGz(srcTarGzFile,destDir); boo = true; } catch (IOException e) { throw new RuntimeException(e); }finally{ //清理操作 if(!boo) deleteDirectory(new File(destDir));//目标文件夹 。清理 } } public static void main(String[] args) throws Exception { XZouTarAndGz jtar = new XZouTarAndGz(); //jtar.testTar(); //jtar.testTarGz(); //jtar.testUnTar(); jtar.testUnTarGz(); } /** * 归档tar文件 * @param file 归档的文件对象 * @param out 输出tar流 * @param dir 相对父目录名称 * @param boo 是否把空目录归档进去 */ public static void tar(File file,TarOutputStream out,String dir,boolean boo) throws IOException{ if(file.isDirectory()){//是目录 File []listFile = file.listFiles();//得出目录下所有的文件对象 if(listFile.length == 0 && boo){//空目录归档 out.putNextEntry(new TarEntry(dir + file.getName() + "/"));//将实体放入输出Tar流中 System.out.println("归档." + dir + file.getName() + "/"); return; }else{ for(File cfile: listFile){ tar(cfile,out,dir + file.getName() + "/",boo);//递归归档 } } }else if(file.isFile()){//是文件 System.out.println("归档." + dir + file.getName() + "/"); byte[] bt = new byte[2048*2]; TarEntry ze = new TarEntry(dir+file.getName());//构建tar实体 //设置压缩前的文件大小 ze.setSize(file.length()); //ze.setName(file.getName());//设置实体名称.使用默认名称 out.putNextEntry(ze);////将实体放入输出Tar流中 FileInputStream fis = null; try{ fis = new FileInputStream(file); int i=0; while((i = fis.read(bt)) != -1) {//循环读出并写入输出Tar流中 out.write(bt, 0, i); } }catch(IOException ex){ throw new IOException("写入归档文件出现异常",ex); }finally{ try{ if (fis != null) fis.close();//关闭输入流 out.closeEntry(); }catch(IOException ex){ throw new IOException("关闭输入流出现异常"); } } } } /** * 解压tar File * @param file 要解压的tar文件对象 * @param outputDir 要解压到某个指定的目录下 * @throws IOException */ public void unTar(File file,String outputDir) throws IOException { TarInputStream tarIn = null; try{ tarIn = new TarInputStream(new FileInputStream(file),1024 * 2); createDirectory(outputDir,null);//创建输出目录 TarEntry entry = null; while( (entry = tarIn.getNextEntry()) != null ){ if(entry.isDirectory()){//是目录 createDirectory(outputDir,entry.getName());//创建空目录 }else{//是文件 File tmpFile = new File(outputDir + "/" + entry.getName()); createDirectory(tmpFile.getParent() + "/",null);//创建输出目录 OutputStream out = null; try{ out = new FileOutputStream(tmpFile); int length = 0; byte[] b = new byte[2048]; while((length = tarIn.read(b)) != -1){ out.write(b, 0, length); } }catch(IOException ex){ throw ex; }finally{ if(out!=null) out.close(); } } } }catch(IOException ex){ throw new IOException("解压归档文件出现异常",ex); } finally{ try{ if(tarIn != null){ tarIn.close(); } }catch(IOException ex){ throw new IOException("关闭tarFile出现异常",ex); } } } /** * 解压tar.gz 文件 * @param file 要解压的tar.gz文件对象 * @param outputDir 要解压到某个指定的目录下 * @throws IOException */ public void unTarGz(File file,String outputDir) throws IOException{ TarInputStream tarIn = null; try{ tarIn = new TarInputStream(new GZIPInputStream( new BufferedInputStream(new FileInputStream(file))), 1024 * 2); createDirectory(outputDir,null);//创建输出目录 TarEntry entry = null; while( (entry = tarIn.getNextEntry()) != null ){ if(entry.isDirectory()){//是目录 createDirectory(outputDir,entry.getName());//创建空目录 }else{//是文件 File tmpFile = new File(outputDir + "/" + entry.getName()); createDirectory(tmpFile.getParent() + "/",null);//创建输出目录 OutputStream out = null; try{ out = new FileOutputStream(tmpFile); int length = 0; byte[] b = new byte[2048]; while((length = tarIn.read(b)) != -1){ out.write(b, 0, length); } }catch(IOException ex){ throw ex; }finally{ if(out!=null) out.close(); } } } }catch(IOException ex){ throw new IOException("解压归档文件出现异常",ex); } finally{ try{ if(tarIn != null){ tarIn.close(); } }catch(IOException ex){ throw new IOException("关闭tarFile出现异常",ex); } } } /** * 构建目录 * @param outputDir * @param subDir */ public void createDirectory(String outputDir,String subDir){ File file = new File(outputDir); if(!(subDir == null || subDir.trim().equals(""))){//子目录不为空 file = new File(outputDir + "/" + subDir); } if(!file.exists()){ file.mkdirs(); } } /** * 清理文件(目录或文件) * @param file */ public void deleteDirectory(File file){ if(file.isFile()){ file.delete();//清理文件 }else{ File list[] = file.listFiles(); if(list!=null){ for(File f: list){ deleteDirectory(f); } file.delete();//清理目录 } } } }
另利用这些,制作了一个java工具实现压缩(zip,tar,tar.gz,tar.bz2)功能.地址是http://lhxctc.iteye.com/blog/709492
- ant.jar (1.3 MB)
- 下载次数: 253
发表评论
-
上传下载的一个工具类
2010-05-29 14:15 2188自己做了一个上传(利用apache-fileupload组件) ... -
String.format一些用法例子、功能强、希望对您有帮组
2010-05-29 14:06 7422String.format是在JDK1.5中新 ... -
Apache POI SpreadSheet的一些简单应用(二)
2009-12-22 10:16 2846我用的版本是 poi-3.5,您可以去官方上下载。希望对大家能 ... -
根据SQL结果集构建动态二维列表展示在excel中
2009-12-08 21:39 2019近期在项目中用到根据SQL结果集构建动态二维列表展示在exce ... -
JDBC读取一些元数据的常用方法
2009-12-07 16:02 5153JDBC读取一些元数据的常用方法,网上有很多这样的DEMO,在 ... -
java中的精确商务计算
2009-12-06 21:58 937由于计算机在计算过程中的舍入关系,普通的计算和工程计算可能会出 ... -
经常使用的一些克隆实现
2009-12-04 20:54 1331在实践中往往需要使用到克隆,您可以使用java本身提供的克隆实 ... -
Apache dbcp 数据源
2009-12-03 16:59 1327使用直连数据库的话效率比较低,人们就想出另外一种概念:数据库连 ... -
Apache POI SpreadSheet的一些简单应用(一)
2009-12-03 16:35 1872我用的版本是 poi-3.5,您可以去官方上下载。 希望对大家 ... -
java.text包中的DateFormat,NumberFormat一些用法
2009-12-03 15:36 2491import java.text.DateForm ... -
java类中多个时间类型的转换处理
2009-12-03 15:06 1500public class XZou { ... -
List列表中删除指定的多个对象,利用下标来实现
2009-12-03 14:47 2133import java.util.ArrayLis ... -
Excel中列号对应数字或数字对应列号
2009-12-03 14:38 2555public class XZou { p ...
相关推荐
apache-ant-1.9.16-bin.zip
nt是Java的生成工具,是Apache的核心项目; Ant类似于Unix中的Make工具,都是用来编译、生成; Ant是跨平台的,而Make不能; Ant的主要目的就是把你想做的事情自动化,不用你手动一步一步做,因为里面内置了javac...
apache-ant-1.9.1-bin.tar.gz
Ant is distributed as zip, tar.gz and tar.bz2 archives - the contents are the same. Please note that the tar.* archives contain file names longer than 100 characters and have been created using GNU ...
apache-ant-1.10.5-bin.tar.gz 本地下载 云盘下载 官网下载
全名:apache-ant-1.10.11-bin.zip。在运行时要求最低java 8
Apache Ant是基于Java的构建工具。ant/binaries
包含:apache-ant-1.9.14-bin.tar.gz、apache-maven-3.5.4-bin.tar.gz、apache-tomcat-6.0.41.tar.gz、jdk1-8u231-linux-x64.tar.gz、protobuf-2.5.0.tar.gz、hadoop-2.7.1-src.tar.gz
java本身自带有zip格式的压缩解压api,但是对于含有中文的压缩文件无能为力,好在还有apache的ant可以解决zip文件的中文乱码问题。mucommander是一个可以支持zip、gzip、rar、tar、iso等格式的全能工具,这个包是...
apache-ant-1.9.9-bin.tar.gz apache-maven-3.0.5-bin.tar.gz hadoop-2.7.2-src.tar.gz protobuf-2.5.0.tar.gz
nutch2.2.1安装步骤,需要自己下载以下软件: apache-ant-1.10.5-bin.tar.gz apache-nutch-2.2.1-src.tar.gz apache-tomcat-8.5.39.tar.gz jdk-8u201-linux-x64.tar.gz solr-4.10.3.zip
内含1.apache-ant-1.9.13-bin.zip 2.jdk-6u45-linux-x64.bin 3.openjdk-7u6-fcs-src-b24-28_aug_2012.zip 4.freetype-2.3.12.tar.gz