java压缩文件夹

public static void main(String[] args) throws Exception {
		File file= new File("d:"+File.separator+"test");
		File zipFile = new File("d:"+File.separator+"1.zip");
		InputStream input = null;
		ZipOutputStream zipOut=null;
		zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
		zipOut.setComment("www.baidu.com");
		int temp=0;
		if(file.isDirectory()){
			File lists[] = file.listFiles();
			for(int i=0;i<lists.length;i++){
				input = new FileInputStream(lists[i]);
				zipOut.putNextEntry(new ZipEntry(file.getName()+File.separator+lists[i].getName()));
				while((temp=input.read())!=-1){
					zipOut.write(temp);
				}
				input.close();
			}
		}
		zipOut.close();

		
	}

你可能感兴趣的:(java压缩文件夹)