?Java?,?????java.util.zip
?????????????????????????,???????Java??ZIP????????????
import java.io.*; import java.util.zip.*; public class UnzipAndVerify { public static void main(String[] args) { String zipFilePath = "path/to/your/zipfile.zip"; String destDirectory = "path/to/your/destination/directory"; String expectedChecksum = "expected_checksum_value"; // ??MD5?SHA-1??????????? try { // ???? unzip(zipFilePath, destDirectory); // ??????????? String actualChecksum = calculateChecksum(destDirectory + File.separator + "your_file_to_verify"); // ????? if (expectedChecksum.equals(actualChecksum)) { System.out.println("??????!"); } else { System.out.println("??????!"); } } catch (IOException | NoSuchAlgorithmException e) { e.printStackTrace(); } } public static void unzip(String zipFilePath, String destDirectory) throws IOException { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { extractFile(zipIn, filePath); } else { File dir = new File(filePath); dir.mkdirs(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); } public static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } public static String calculateChecksum(String filePath) throws IOException, NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); // ???SHA-1??? FileInputStream fis = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { md.update(buffer, 0, bytesRead); } fis.close(); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("x", b)); } return sb.toString(); } }
?????,??????ZIP??,??????????MD5??????,????????????????????????????,????????;??,??????????,???????????zipFilePath
?destDirectory
?expectedChecksum
?????