java.util.zip
来源:互联网 发布:新浪数据nba 编辑:程序博客网 时间:2024/06/05 05:30
(一)压缩单个文件
public static void main(String[] args) { //压缩单个文件 //要压缩的文件路径 String filepath="D:\\markavip\\测试.txt"; //文件压缩之后的路径 String zippath="D:\\markavip\\test.zip"; zipFile(filepath,zippath); } public static void zipFile(String filepath,String zippath){ InputStream input=null; //压缩 ZipOutputStream zipOut=null; try{ File file=new File(filepath); File zipFile=new File(zippath); input=new FileInputStream(file); zipOut=new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.putNextEntry(new ZipEntry(file.getName())); int tmp=0; byte[] buffer=new byte[1024]; while((tmp=input.read(buffer,0,1024))!=-1){ zipOut.write(buffer,0,tmp); } }catch(Exception e){ e.printStackTrace(); }finally { try{ input.close(); zipOut.close(); }catch (Exception ex){ ex.printStackTrace(); } } }
(二)、压缩一个文件夹(包含多个文件)
public static void main(String[] args) { //文件夹路径 String filepath="D:\\markavip\\"; //压缩后的文件夹路径 String zippath="D:\\markTest.zip"; zipDirFile(filepath,zippath); } public static void zipDirFile(String filepath,String zippath){ try{ File file=new File(filepath); File zipFile=new File(zippath); InputStream input=null; ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)); if(file.isDirectory()){ File[] files=file.listFiles(); for(int i=0;i<files.length;i++){ input=new FileInputStream(files[i]); zipOut.putNextEntry(new ZipEntry(file.getName()+File.separator+files[i].getName())); int tmp=0; byte[] buf=new byte[1024]; while((tmp=input.read(buf,0,1024))!=-1){ zipOut.write(buf,0,tmp); } input.close(); } }else{ //调用单个压缩文件 ZipOneFile.zipFile(filepath,zippath); } zipOut.close(); }catch (Exception e){ e.printStackTrace(); } }
(三)解压单个文件
public static void main(String[] args) { //压缩的文件路径 String zippath="D:\\markavip\\test.zip"; //解压之后的文件路径 String outfilepath="D:\\markavip\\one.txt"; //压缩文件中的文件名 String filename="测试.txt"; zipContraFile(zippath,outfilepath,filename); } public static void zipContraFile(String zippath,String outfilepath,String filename){ try{ File file=new File(zippath); File outFile=new File(outfilepath); ZipFile zipFile=new ZipFile(file); ZipEntry entry=zipFile.getEntry(filename); InputStream input=zipFile.getInputStream(entry); OutputStream output=new FileOutputStream(outFile); int tmp=0; byte[] buf=new byte[1024]; while((tmp=input.read(buf,0,1024))!=-1){ output.write(buf,0,tmp); } input.close(); output.close(); }catch (Exception e){ e.printStackTrace(); }finally { } }
(四)、解压一个文件夹(内含多个文件)
public static void main(String[] args) { //压缩文件路径 String zippath="D:\\markTest.zip"; //解压后的路径 String outfilepath="D:\\YOHO"; zipContraMultiFile(zippath,outfilepath); } public static void zipContraMultiFile(String zippath,String outfilepath ){ ZipInputStream zipInput=null; ZipFile zipFile=null; InputStream input=null; OutputStream output=null; try{ File file=new File(zippath); File outFile=null; zipFile=new ZipFile(file); zipInput=new ZipInputStream(new FileInputStream(file)); ZipEntry entry=null; while ((entry=zipInput.getNextEntry())!=null){ outFile=new File(outfilepath+File.separator+entry.getName()); if(!outFile.getParentFile().exists()){ outFile.getParentFile().mkdir(); } if(!outFile.exists()){ outFile.createNewFile(); } input=zipFile.getInputStream(entry); output=new FileOutputStream(outFile); int tmp=0; byte[] buf=new byte[1024]; while((tmp=input.read(buf,0,1024))!=-1){ output.write(buf,0,tmp); } } }catch (Exception e){ e.printStackTrace(); }finally { try{ input.close(); output.close(); zipInput.close(); zipFile.close(); }catch (Exception e){ e.printStackTrace(); } } }
参考文章:
http://www.2cto.com/kf/201412/363662.html
http://www.cnblogs.com/chiangfai/p/5888141.html
http://blog.csdn.net/u011423071/article/details/52043702
阅读全文
0 0
- java.util.zip
- java.util.zip
- java.util.zip.ZipException
- java.util.zip对zip文件解压
- java.util.zip.ZipFile翻译
- java.util.zip.ZipInputStream翻译
- java.util.zip.ZipOutputStream翻译
- java.util.zip.ZipException翻译
- java.util.zip.ZipEntry翻译
- java.util.zip.CRC32翻译
- java.util.zip.Adler32翻译
- java.util.zip.Checksum翻译
- java.util.zip.CheckedInputStream翻译
- java.util.zip.CheckedOutputStream翻译
- java.util.zip.DataFormatException翻译
- java.util.zip.DeflaterOutputStream翻译
- java.util.zip.InflaterInputStream翻译
- java.util.zip.GZIPInputStream翻译
- junit单元测试
- Android中沉浸式模式
- rdd是什么
- Swiper
- springboot 的application.properties 一些常用的属性
- java.util.zip
- 安卓之动画制作
- Java之集合、泛型
- 关于intent的学习
- 万能适配器(一)
- ubuntu16.04安装中文
- 为android系统添加USB AUDIO设备的放音和录音功能
- SolrCore配置
- lora_gateway-master\util_tx_continuous---Makefile分析笔记