| 30 | } |
| 31 | |
| 32 | public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException { |
| 33 | if (inFile.isDirectory()) { |
| 34 | File[] files = inFile.listFiles(); |
| 35 | if (files == null || files.length == 0) { |
| 36 | String entryName = dir + "/"; |
| 37 | zos.putNextEntry(new ZipEntry(entryName)); |
| 38 | return; |
| 39 | } |
| 40 | for (File file : files) { |
| 41 | String entryName = dir + "/" + file.getName(); |
| 42 | if (file.isDirectory()) { |
| 43 | zipFile(file, zos, entryName); |
| 44 | } else { |
| 45 | ZipEntry entry = new ZipEntry(entryName); |
| 46 | zos.putNextEntry(entry); |
| 47 | try (InputStream is = new FileInputStream(file)) { |
| 48 | int len = 0; |
| 49 | while ((len = is.read()) != -1) { |
| 50 | zos.write(len); |
| 51 | } |
| 52 | } catch (IOException e) { |
| 53 | throw e; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } else { |
| 58 | String entryName = dir + "/" + inFile.getName(); |
| 59 | ZipEntry entry = new ZipEntry(entryName); |
| 60 | zos.putNextEntry(entry); |
| 61 | try (InputStream is = new FileInputStream(inFile)) { |
| 62 | int len = 0; |
| 63 | while ((len = is.read()) != -1) { |
| 64 | zos.write(len); |
| 65 | } |
| 66 | } catch (IOException e) { |
| 67 | throw e; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 文档解压 |