java 压缩文件
Posted 张玉坤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 压缩文件相关的知识,希望对你有一定的参考价值。
一段 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 压缩文件的主要内容,如果未能解决你的问题,请参考以下文章