java use ZipOutputStream batch file compression, and file compression were placed in different folders

cn.cnnho.backstage.controller Package;
Import of java.util.ArrayList;
Import java.util.List;
Import the java.util.zip *;.
Import the java.io. *;
public class the Test {
/ **
* a compressed file a ZIP
* @param in the input file
* @param out output ZIP name
* @param outEntry zip entry names in
* /
public static void ZIP (in String, the ZipOutputStream OUT, String outEntry) {
file INF = new new file ( in);
the FileInputStream INS;
the try {
IF (inf.exists ()) {
INS = new new the FileInputStream (INF);

// folder on demand to dynamically populate
out.putNextEntry (new ZipEntry ( "folder" + "/" + outEntry));
int B;
(! (B = ins.read ()) = -1) {the while
OUT. Write (B);
}
ins.close ();
} the else {
// project requires administering it in the absence of prompt.
System.out.println ( "system can not find the file specified:" + in);
}

} the catch (a FileNotFoundException E) {
e.printStackTrace ();
} the catch (IOException E2) {
e2.printStackTrace ();
} the catch (Exception E3) {
e3.printStackTrace ();
}
}

/ **
*
* @param out output ZIP name
* @param in the input directory
entries * @param inEntrys set input
* /
public static void zip(String out,String in,List<String> inEntrys) {
try {
OutputStream zipFileName_fileOutputStream = new FileOutputStream(out);
ZipOutputStream zipOutputStream = new ZipOutputStream(zipFileName_fileOutputStream);
for(String inEntry:inEntrys){
zip(in+inEntry,zipOutputStream,inEntry);
}
zipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {

List<String> inEntrys=new ArrayList<String>();
inEntrys.add("a.txt");
inEntrys.add("b.txt");
//可以加一个实际没有的文件试试
inEntrys.add("c.txt");

zip("d:\\a\\a3.zip","d:\\a\\",inEntrys);

}
}

Guess you like

Origin www.cnblogs.com/guangxiang/p/11427816.html