java简单的压缩方法(zip压缩)
来源:互联网 发布:草图大师是什么软件 编辑:程序博客网 时间:2024/04/29 16:23
前几天有个功能需要上传文件到FTP,由于文件过大,想到了压缩,发现了一个简单的java压缩方法。
ZipOutputSream等类都是java库自带的,下面上代码。
public class yasuo {public File doZip(String sourceDir, String zipFilePath) throws IOException {File file = new File(sourceDir);File zipFile = new File(zipFilePath);ZipOutputStream zos = null;try {// 创建写出流操作OutputStream os = new FileOutputStream(zipFile);BufferedOutputStream bos = new BufferedOutputStream(os);zos = new ZipOutputStream(bos);String basePath = null;// 获取目录if (file.isDirectory()) {basePath = file.getPath();} else {basePath = file.getParent();}zipFile(file, basePath, zos);} finally {if (zos != null) {zos.closeEntry();zos.close();}}return zipFile;}/** * @param source 源文件 * @param basePath * @param zos */@SuppressWarnings("resource")private void zipFile(File source, String basePath, ZipOutputStream zos)throws IOException {File[] files = null;if (source.isDirectory()) {files = source.listFiles();} else {files = new File[1];files[0] = source;}InputStream is = null;String pathName;byte[] buf = new byte[1024];int length = 0;try {for (File file : files) {if (file.isDirectory()) {pathName = file.getPath().substring(basePath.length())+ "/";zos.putNextEntry(new ZipEntry(pathName));zipFile(file, basePath, zos);} else {pathName = file.getPath().substring(basePath.length());is = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(is);zos.putNextEntry(new ZipEntry(pathName));while ((length = bis.read(buf)) > 0) {zos.write(buf, 0, length);}}}} finally {if (is != null) {is.close();}}} //实现public static void main(String args[]){yasuo yasuo2=new yasuo();try {yasuo2.doZip("E:\\abc.log","E:\\test.zip");System.out.println("success");} catch (IOException e) {System.out.println("wrong");e.printStackTrace();}}}
1 0
- java简单的压缩方法(zip压缩)
- java的zip压缩
- java 实现zip压缩的三种好方法
- ZIP压缩的JAVA代码
- 简单的Zip压缩与解压
- java 解压缩zip 压缩zip
- Java Zip压缩
- java 压缩zip
- JAVA zip 压缩
- Java压缩zip文件
- java zip压缩
- JAVA zip压缩
- JAVA压缩/解压ZIP
- java zip压缩
- Java压缩/解压ZIP
- java zip压缩解压
- java zip压缩。解压缩
- java zip压缩
- Android进阶_通过MainActivity跳转应用
- JQuery validate 验证数据是否与数据库中数据重复
- android获取wifi信号强度
- ngnix+openssl+pcre+php
- 很高兴我的csdn博客开通
- java简单的压缩方法(zip压缩)
- html的meta总结,html标签中meta属性使用介绍(转)
- 控制输出java_control-output
- eclipse创建web项目和配置tomcat
- SQL Server2008中CROSS APPLY的应用范例(三) - 将一个或多个字段内用逗号分隔的内容分成多条记录
- jQuery循环N多个form把json传入后台
- Exception in thread "main" org.hibernate.SessionException: Session is closed!
- centos7 firewalll防火墙中的设置
- 关于微博api授权问题求一个详细步奏