日期:2014-05-20 浏览次数:21224 次
void zip() {
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
String[] file=null;
try {
System.out.print("请输入压缩包数量:");
int sum=Integer.parseInt(in1.readLine());
file = new String[sum];
int i = 0;
do {
System.out.println("请输入需要压缩的第"+(i+1)+"文件的文件名:");
file[i]=in1.readLine();
i++;
} while (i<=sum-1);
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(null!=in1){
in1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
ZipOutputStream out1 = null;
BufferedInputStream in2 = null;
try {
for (int j = 0; j <file.length; j++) {
out1 = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file[j]+j+"_b.zip")));
in2 = new BufferedInputStream(new FileInputStream(file[j]));
byte[] temp=new byte[in2.available()];
out1.putNextEntry(new ZipEntry(file[j]));
in2.read(temp);
out1.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(null!=in2){
in2.close();
}
if(null!=out1){
out1.close();
}
System.out.println("文件另外压缩复制完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}