複数ファイルの圧縮(中国語の問題)

2269 ワード


圧縮されたファイルには、元のファイルパス情報が含まれている場合があります.二、圧縮後に経路情報を含まない.
一、

public static void main(String[] s) {
		 //           
		String[] source = new String[]{"C:\\c\\a.txt", "C:\\c\\b.txt"};
		//               
		byte[] buf = new byte[1024];
		try {
		//  zip  
		String target = "C:\\target.zip";
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
		out.setEncoding("GBK");//        
		//             
		for (int i=0; i<source.length; i++) {
		FileInputStream in = new FileInputStream(source[i]);
		//   zip    c
		out.putNextEntry(new ZipEntry(source[i]));
		//               ZIP  
		int len;
		while ((len = in.read(buf)) > 0) {
		out.write(buf, 0, len);
		}
		//     
		out.closeEntry();
		in.close();
		}
		// Complete the ZIP file
		out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

二、

public static void main(String[] s) {
		 //           
		String[] source = new String[]{"C:\\c\\a.txt", "C:\\c\\b.txt"};
        File[] sourcefile = new File[source.length];
        for(int i=0;i<sourcefile.length;i++){//    
        	sourcefile[i]= new File(source[i]);
        }
		//               
		byte[] buf = new byte[1024];
		try {
		//  zip  
		String target = "C:\\target.zip";
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
		out.setEncoding("GBK");//        
		//             
		for (int i=0; i<sourcefile.length; i++) {
		FileInputStream in = new FileInputStream(sourcefile[i]);
		//   zip    c
		out.putNextEntry(new ZipEntry(sourcefile[i].getName()));
		//               ZIP  
		int len;
		while ((len = in.read(buf)) > 0) {
		out.write(buf, 0, len);
		}
		//     
		out.closeEntry();
		in.close();
		}
		// Complete the ZIP file
		out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}