程式碼:
public class ZipUtil { /** * 压缩文件/文件夹 * */ public static void compress(String srcFilePath, String destFilePath) { File src = new File(srcFilePath); if (!src.exists()) { throw new RuntimeException(srcFilePath + "不存在"); } File zipFile = new File(destFilePath); try { FileOutputStream fos = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); String baseDir = ""; compressbyType(src, zos, baseDir); zos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void compressbyType(File src, ZipOutputStream zos, String baseDir) { if (!src.exists()) return; System.out.println("压缩" + baseDir + src.getName()); if (src.isFile()) { compressFile(src, zos, baseDir); } else if (src.isDirectory()) { compressDir(src, zos, baseDir); } } /** * 压缩文件 */ private static void compressFile(File file, ZipOutputStream zos, String baseDir) { if (!file.exists()) return; try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(baseDir + file.getName()); zos.putNextEntry(entry); int count; byte[] buf = new byte[8019]; while ((count = bis.read(buf)) != -1) { zos.write(buf, 0, count); } bis.close(); } catch (Exception e) { // TODO: handle exception } } /** * 压缩文件夹 * */ private static void compressDir(File dir, ZipOutputStream zos, String baseDir) { if (!dir.exists()) return; File[] files = dir.listFiles(); if(files.length == 0){ try { zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator)); } catch (IOException e) { e.printStackTrace(); } } for (File file : files) { compressbyType(file, zos, baseDir + dir.getName() + File.separator); } } /** * 解压文件/文件夹 */ public static void decompress(String srcPath, String dest) throws Exception { File file = new File(srcPath); if (!file.exists()) { throw new RuntimeException(srcPath + "所指文件不存在"); } ZipFile zf = new ZipFile(file); Enumeration entries = zf.entries(); ZipEntry entry = null; while (entries.hasMoreElements()) { entry = (ZipEntry) entries.nextElement(); System.out.println("解压" + entry.getName()); if (entry.isDirectory()) { String dirPath = dest + File.separator + entry.getName(); File dir = new File(dirPath); dir.mkdirs(); } else { // 表示文件 File f = new File(dest + File.separator + entry.getName()); if (!f.exists()) { //String dirs = FileUtils.getParentPath(f); String dirs = f.getParent(); File parentDir = new File(dirs); parentDir.mkdirs(); } f.createNewFile(); // 将压缩文件内容写入到这个文件中 InputStream is = zf.getInputStream(entry); FileOutputStream fos = new FileOutputStream(f); int count; byte[] buf = new byte[8192]; while ((count = is.read(buf)) != -1) { fos.write(buf, 0, count); } is.close(); fos.close(); } } } public static void main(String[] args) throws Exception { // compress("/Users/liyinlong/lyl/test","/Users/liyinlong/lyl/test.zip"); decompress("/Users/liyinlong/lyl/test.zip", "/Users/liyinlong/lyl/test"); } }
以上是Java怎麼壓縮和解壓zip文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。

Java的強類型系統通過類型安全、統一的類型轉換和多態性確保了平台獨立性。 1)類型安全在編譯時進行類型檢查,避免運行時錯誤;2)統一的類型轉換規則在所有平台上一致;3)多態性和接口機制使代碼在不同平台上行為一致。

JNI會破壞Java的平台獨立性。 1)JNI需要特定平台的本地庫,2)本地代碼需在目標平台編譯和鏈接,3)不同版本的操作系統或JVM可能需要不同的本地庫版本,4)本地代碼可能引入安全漏洞或導致程序崩潰。

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境