public static String[] getZipList(String zipFile) throws ZipException, IOException { File file = new File(zipFile); ZipFile zip = new ZipFile(file); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); ArrayList<String> files = new ArrayList<String>(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); files.add(currentEntry); } return files.toArray(new String[files.size()]); }
private static byte[] getZipDigest(ZipFile zipFile, ZipEntry ze, boolean zip) throws ZipException, IOException, Exception { System.out.println("z2e: " + ze.toString()); InputStream is = null; if (zip) is = zipFile.getInputStream(ze); else is = new FileInputStream(testDirName + ze.toString()); MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] buf = new byte[1024]; int count; while ((count = is.read(buf)) != -1) { System.out.println("Count: " + count); sha.update(buf, 0, count); } is.close(); return sha.digest(); }
public static void extract(String zipFile, String newPath, boolean overwrite) throws ZipException, IOException { File file = new File(zipFile); ZipFile zip = new ZipFile(file); new File(newPath).mkdir(); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(newPath, currentEntry); // destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { if (!overwrite && destFile.exists()) continue; BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } close(dest); close(is); } } }