Java压缩类库的使用 3.Apache Ant中的打包、压缩类库
	
    
    
	  inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish)。
  这里需要关注的是BZIP2格式,经过测试,总是无法正确压缩,原因未知,而apache commons bzip2格式的文件压缩正常。(来源:http://blog.csdn.net/inkfish)
Ant ZIP压缩:(来源:http://blog.csdn.net/inkfish)
package study.inkfish.compress;
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.Enumeration;
import org.apache.commons.io.IOUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class AntZipCompress extends Compress {
	@Override
	protected void doCompress(File srcFile, File destFile) throws IOException {
		ZipOutputStream zout = null;
		InputStream is = null;
		try {
			is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
			zout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
			zout.putNextEntry(new ZipEntry(srcFile.getName()));
			IOUtils.copy(is, zout);
			zout.closeEntry();
		} finally {
			IOUtils.closeQuietly(is);
			IOUtils.closeQuietly(zout);
		}
	}
	@Override
	protected void doDecompress(File srcFile, File destDir) throws IOException {
		ZipFile zipFile = new ZipFile(srcFile);
		try {
			@SuppressWarnings("unchecked")
			Enumeration<ZipEntry> enums = zipFile.getEntries();
			while (enums.hasMoreElements()) {
				ZipEntry entry = enums.nextElement();
				InputStream is = new BufferedInputStream(zipFile.getInputStream(entry), bufferLen);
				OutputStream os = null;
				try {
					os = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), bufferLen);
					IOUtils.copy(is, os);
				} finally {
					IOUtils.closeQuietly(is);
					IOUtils.clo
    
     
	
	
    
    
	相关文档:
        
    
     
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
	
    
        
    
    十年前,Sun微系统公司将Java搬到了世人面前,这是首次协助企业建立具有前瞻性的思想的一款软件,随后Java迅猛扩散,深入到计算机业的几乎每个角落。这项技术的幕后英雄,就是本文采访的James Gosling。 
上个世纪90年代初,Gosling发起并领导了一个名为Green的项目,此项目最终演变为Java。Java 的基本理念是创造一种可以 ......
	
    
        
    
      
在你的代码里调用了一些资源文件,如图片,音乐等,在调试环境或单独运行的时候可以正常显示或播放,而一旦打包到jar文件中,这些东东就再也出不来了,除非把这个jar放到原来未打包以前的目录下,但通常jar是单独发布的。
[关键字] java jar文件包 资源
  可能有不少初学者会有这样的困惑:在你的代码里调用了 ......
	
    
        
    
      inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish)。
  压缩是编程中常见的技巧,多用于大文件压缩,数据流压缩等。在Java类库中,内置了jar、ZIP、GZIP、ZLIB等的支持(见java.util.zip、java.util.jar包)。另外在Apache项目下Ant中ant.jar的org.apache.tools.tar、org.apache.tool ......