1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| package zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream;
public class ArchiveUtils {
private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception { if (file == null) { return; }
if (file.isFile()) { int count, bufferLen = 1024; byte data[] = new byte[bufferLen];
String subPath = file.getAbsolutePath(); int index = subPath.indexOf(srcRootDir); if (index != -1) { subPath = subPath.substring(srcRootDir.length() + File.separator.length()); } ZipEntry entry = new ZipEntry(subPath); zos.putNextEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while ((count = bis.read(data, 0, bufferLen)) != -1) { zos.write(data, 0, count); } bis.close(); zos.closeEntry(); } else { File[] childFileList = file.listFiles(); for (int n = 0; n < childFileList.length; n++) { childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath()); zip(srcRootDir, childFileList[n], zos); } } }
public static void zip(String srcPath, String zipPath, String zipFileName) throws Exception { if (srcPath == null || zipPath==null || zipFileName==null) { throw new IllegalArgumentException("NULL"); } CheckedOutputStream cos = null; ZipOutputStream zos = null; try { File srcFile = new File(srcPath);
if (srcFile.isDirectory() && zipPath.indexOf(srcPath) != -1) { throw new IllegalArgumentException("zipPath must not be the child directory of srcPath."); }
File zipDir = new File(zipPath); if (!zipDir.exists() || !zipDir.isDirectory()) { zipDir.mkdirs(); }
String zipFilePath = zipPath + File.separator + zipFileName; File zipFile = new File(zipFilePath); if (zipFile.exists()) { SecurityManager securityManager = new SecurityManager(); securityManager.checkDelete(zipFilePath); zipFile.delete(); }
cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32()); zos = new ZipOutputStream(cos);
String srcRootDir = srcPath; if (srcFile.isFile()) { int index = srcPath.lastIndexOf(File.separator); if (index != -1) { srcRootDir = srcPath.substring(0, index); } } zip(srcRootDir, srcFile, zos); zos.flush(); } catch (Exception e) { throw e; } finally { try { if (zos != null) { zos.close(); } } catch (Exception e) { e.printStackTrace(); } } }
@SuppressWarnings("unchecked") public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws IOException { if (zipFilePath==null|| !new File(zipFilePath).exists()) { return; } File zipFile = new File(zipFilePath); if (includeZipFileName) { String fileName = zipFile.getName(); if (fileName!=null) { fileName = fileName.substring(0, fileName.lastIndexOf(".")); } unzipFilePath = unzipFilePath + File.separator + fileName; } File unzipFileDir = new File(unzipFilePath); if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) { unzipFileDir.mkdirs(); }
ZipEntry entry = null; String entryFilePath = null, entryDirPath = null; File entryFile = null, entryDir = null; int index = 0, count = 0, bufferSize = 1024; byte[] buffer = new byte[bufferSize]; BufferedInputStream bis = null; BufferedOutputStream bos = null; ZipFile zip = new ZipFile(zipFile); Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries(); while (entries.hasMoreElements()) { entry = entries.nextElement(); entryFilePath = unzipFilePath + File.separator + entry.getName(); index = entryFilePath.lastIndexOf(File.separator); if (index != -1) { entryDirPath = entryFilePath.substring(0, index); } else { entryDirPath = ""; } entryDir = new File(entryDirPath); if (!entryDir.exists() || !entryDir.isDirectory()) { entryDir.mkdirs(); } entryFile = new File(entryFilePath); if (entryFile.getAbsolutePath().equals(entryDir.getAbsolutePath())) continue; if (entryFile.exists()) { SecurityManager securityManager = new SecurityManager(); securityManager.checkDelete(entryFilePath); entryFile.delete(); }
bos = new BufferedOutputStream(new FileOutputStream(entryFile)); bis = new BufferedInputStream(zip.getInputStream(entry)); while ((count = bis.read(buffer, 0, bufferSize)) != -1) { bos.write(buffer, 0, count); } bos.flush(); bos.close(); } } public static void main(String[] args) throws IOException { unzip("C:\\Users\\Administrator\\Desktop\\A0_9D_C1_EC_E2_1E_2019-05-14-11-46-58.zip","E:\\test\\",false); } }
|