Java压缩类库的使用-2.JDK中的打包、压缩类库

  inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish)。

  这里忽略了jar,因为jar实质上属于zip压缩。(来源:http://blog.csdn.net/inkfish)

JDK ZLIB压缩:(来源: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.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; public class JdkZLIBCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { OutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); IOUtils.copy(is, out); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { InputStream is = null; OutputStream os = null; try { File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.toString())); is = new InflaterInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

JDK 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.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.io.IOUtils; public class JdkZipCompress 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 { ZipInputStream is = null; try { is = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); ZipEntry entry = null; while ((entry = is.getNextEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); is.closeEntry(); } else { OutputStream os = null; try { os = new BufferedOutputStream( new FileOutputStream(new File(destDir, entry.getName())), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } is.closeEntry(); } } } finally { IOUtils.closeQuietly(is); } } }

JDK GZIP压缩:(来源: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.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; public class JdkGZIPCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { OutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); IOUtils.copy(is, out); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { InputStream is = null; OutputStream os = null; try { File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.toString())); is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

注:org.apache.commons.io包为Apache common io,项目首页:http://commons.apache.org/io/ ,提供了IO操作的很多方便的方法,基于Apache 2.0 License,可用于商业用途。(来源:http://blog.csdn.net/inkfish)

上一篇:Extjs encode decode


下一篇:UVA - 11624 Fire!(多源bfs+双bfs)