码迷,mamicode.com
首页 > 编程语言 > 详细

java 压缩文件

时间:2018-08-09 14:00:13      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:top   new   tor   write   min   str   ips   file   rect   

一段 java  zip  压缩的代码

 

 

                File dir = new File("C:\\Users\\Administrator\\Desktop\\zipsource");
		String zipFile = "C:\\Users\\Administrator\\Desktop\\test2.zip";
		
		
		OutputStream outputStream = new FileOutputStream(new File(zipFile));
		ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
		

		byte[] buffer = new byte[1024];
		int len  = 0;
		
		
		if( dir.isDirectory() ) {
			 File[]  files = dir.listFiles();
			for(File fileItem  :files) {
				FileInputStream fileItemInputStream = new FileInputStream( fileItem );
				ZipEntry zipEntry = new ZipEntry( fileItem.getName() );
				zipOutputStream.putNextEntry( zipEntry );
				while( (len = IOUtils.read(fileItemInputStream, buffer)) != 0 ) {
					zipOutputStream.write( buffer,0,len );
				}
				fileItemInputStream.close();
			}
		}
		zipOutputStream.flush();
		zipOutputStream.close();

  

java 压缩文件

标签:top   new   tor   write   min   str   ips   file   rect   

原文地址:https://www.cnblogs.com/cxygg/p/9448166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!