java.io.zip folder

// zip folder
	//First read the file with the input stream, and then use the compressed stream to output the file
	public static void main(String[] args) throws Exception {
		File file = new File("d:" + File.separator + "myDir");// 源文件
		File zipFile = new File("d:" + File.separator + "2.zip");// The path of the compressed file

		InputStream inputStream = null;
		ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
		zipOutputStream.setComment("compressed comment");

		if (file.isDirectory()) {//If there is a directory in the file
			
			File[] listFiles = file.listFiles();//List all files
			for (int i = 0; i < listFiles.length; i++) {
				inputStream = new FileInputStream(listFiles[i]);//Set the input stream for each file
				
				// Each subfile of a compressed file is represented by a ZipEntry
				// Need to set the name for each compressed file
				zipOutputStream.putNextEntry(new ZipEntry(file.getName() + File.separator + listFiles[i].getName()));
				int temp = 0;
				
				while ((temp = inputStream.read()) != -1) {// read content
					zipOutputStream.write(temp);// Compress the output content
				}
				inputStream.close();   
			}
		}
		zipOutputStream.close();
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326952552&siteId=291194637