Java实现zip解压缩目录中的所有文件
import java.io.*;
02 import java.util.*;
03 import java.util.zip.*;
04 public class FolderUnzip {
05 private static String sourcepath="D:\\tmp";
06 private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath));
07 private static List<String>folderList2=new ArrayList<String>(Arrays.asList("E:\\tt"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator))));
08 private static FileInputStream fis = null;
09 private static FileOutputStream fos = null;
10 private static ZipInputStream zipin = null;
11 public static void main(String[] args) {
12 for (int j = 0; j < folderList.size(); j++) {
13 new File(folderList2.get(j)).mkdirs();
14 String[] file = new File(folderList.get(j)).list();
15 File temp = null;
16 for (int i = 0; i < file.length; i++) {
17 if (folderList.get(j).endsWith(File.separator))
18 temp = new File(folderList.get(j), file[i]);
19 else
20 temp = new File(folderList.get(j), file[i]);
21 File originalFile = null;
22 if (temp.getName().endsWith(".zip"))
23 originalFile = new File(folderList2.get(j), temp.getName()
24 .substring(0, temp.getName().lastIndexOf('.')));
25 if (temp.isFile() && !originalFile.exists()) {
26 try {
27 fis = new FileInputStream(temp);
28 zipin = new ZipInputStream(fis);
29 ZipEntry entry = zipin.getNextEntry();
30 fos = new FileOutputStream(new File(folderList2.get(j),entry.getName()));
31 byte[] buffer = new byte[20480];
32 int nNumber;
33 while ((nNumber = zipin.read(buffer, 0, buffer.length)) != -1)
34 fos.write(buffer, 0, nNumber);
35 fos.flush();
36 } catch (IOException e) {
37 continue;
38 } finally {
39 try {
40 zipin.close();
41 fos.close();
42 fis.close();
43 } catch (IOException e) {
44 }
45 }
46 } else if (temp.isDirectory()) {
47 folderList.add(folderList.get(j) + File.separator + file[i]);
48 folderList2.add(folderList2.get(j) + File.separator+ file[i]);
49 }
50 }
51 }
52 }
53 }
Java实现Zip压缩目录中的所有文件
import java.io.*;
02 import java.util.*;
03 import java.util.zip.*;
04 public class FolderZip {
05 private static String sourcepath="C:\\temp";
06 private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath));
07 private static List<String>folderList2=new ArrayList<String>(Arrays.asList("D:\\tmp"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator))));
08 private static FileInputStream fis = null;
09 private static FileOutputStream fos = null;
10 private static ZipOutputStream zipOut = null;
11 public static void main(String[] args) {
12 for (int j = 0; j < folderList.size(); j++) {
13 new File(folderList2.get(j)).mkdirs();
14 String[] file = new File(folderList.get(j)).list();
15 File temp = null;
16 for (int i = 0; i < file.length; i++) {
17 if (folderList.get(j).endsWith(File.separator))
18 temp = new File(folderList.get(j), file[i]);
19 else
20 temp = new File(folderList.get(j), file[i]);
21 File zipFile = new File(folderList2.get(j), temp.getName()
22 + ".zip");
23 if (temp.isFile() && !zipFile.exists())
24 try {
25 fis = new FileInputStream(temp);
26 fos = new FileOutputStream(zipFile);
27 zipOut = new ZipOutputStream(fos);
28 ZipEntry entry = new ZipEntry(temp.getName());
29 zipOut.putNextEntry(entry);
30 int nNumber;
31 byte[] buffer = new byte[20480];
32 while ((nNumber = fis.read(buffer)) != -1)
33 zipOut.write(buffer, 0, nNumber);
34 zipOut.flush();
35 } catch (IOException e) {
36 continue;
37 } finally {
38 try {
39 zipOut.close();
40 fos.close();
41 fis.close();
42 } catch (IOException e) {
43 }
44 }
45 else if (temp.isDirectory()) {
46 folderList
47 .add(folderList.get(j) + File.separator + file[i]);
48 folderList2.add(folderList2.get(j) + File.separator
49 + file[i]);
50 }
51 }
52 }
53 }
54 }