Java壓縮文件夾最實用簡單的方法
Java 有一個很好的類庫來處理 zip 文件。這些類在 java.util.zip 包中可用。以下 Java 示例程序展示了如何使用 java.util.zip 類創(chuàng)建整個文件夾的 zip。我們使用Files.walkFileTree遞歸地瀏覽目錄樹,然后將每個文件添加到新創(chuàng)建的 zip 文件中。請注意,此示例僅適用于 Java 1.7 及更高版本。
import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; // Source code to create a zip file from a given folder // This example program recursively adds all files in the folder // Works only with Java 7 and above public class ZipFolder { public static void main(String[] args) throws Exception { ZipFolder zf = new ZipFolder(); // Use the following paths for windows //String folderToZip = "c:\\demo\\test"; //String zipName = "c:\\demo\\test.zip"; // Linux/mac paths String folderToZip = "/Users/jj/test"; String zipName = "/Users/jj/test.zip"; zf.zipFolder(Paths.get(folderToZip), Paths.get(zipName)); } // Uses java.util.zip to create zip file private void zipFolder(Path sourceFolderPath, Path zipPath) throws Exception { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath.toFile())); Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString())); Files.copy(file, zos); zos.closeEntry(); return FileVisitResult.CONTINUE; } }); zos.close(); } }
在 linux/mac 中,您可以使用以下命令測試新創(chuàng)建的 zip 文件
解壓-t test.zip
實例擴展
//方法1: public void unZip(String zipfile) throws IOException { //檢查是否是zip文件,并判斷文件是否存在 checkFileName(zipfile); long startTime = System.currentTimeMillis(); File zfile=new File(zipfile); //獲取待解壓文件的父路徑 String Parent=zfile.getParent()+"/"; FileInputStream fis=new FileInputStream(zfile); Charset charset = Charset.forName("GBK");//默認UTF-8 // CheckedInputStream cis = new CheckedInputStream(fis,new CRC32()); ZipInputStream zis = new ZipInputStream(fis,charset);// 輸入源zip路徑 ZipEntry entry=null; BufferedOutputStream bos=null; while ((entry=zis.getNextEntry())!=null) { if (entry.isDirectory()) { File filePath=new File(Parent+entry.getName()); //如果目錄不存在,則創(chuàng)建 if (!filePath.exists()) { filePath.mkdirs(); } }else{ FileOutputStream fos=new FileOutputStream(Parent+entry.getName()); bos=new BufferedOutputStream(fos); byte buf[] = new byte[1024]; int len; while ((len = zis.read(buf)) != -1) { bos.write(buf, 0, len); } zis.closeEntry(); //關(guān)閉的時候會刷新 bos.close(); } } zis.close(); long endTime = System.currentTimeMillis(); System.out.println("解壓完成!所需時間為:"+(endTime-startTime)+"ms"); // System.out.println("校驗和:"+cis.getChecksum().getValue()); } private void checkFileName(String name) { //文件是否存在 if (!new File(name).exists()) { System.err.println("要解壓的文件不存在!"); System.exit(1); } // 判斷是否是zip文件 int index = name.lastIndexOf("."); String str=name.substring(index+1); if (!"zip".equalsIgnoreCase(str)) { System.err.println("不是zip文件,無法解壓!"); System.exit(1); } }
到此這篇關(guān)于Java壓縮文件夾最實用簡單的方法的文章就介紹到這了,更多相關(guān)Java壓縮文件夾的方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中@Controller和@RestController的區(qū)別詳解
這篇文章主要介紹了Spring中@Controller和@RestController的區(qū)別詳解,@RestController?是?@Controller?和?@ResponseBody?的結(jié)合體,單獨使用?@RestController?的效果與?@Controller?和?@ResponseBody?二者同時使用的效果相同,需要的朋友可以參考下2023-10-10Java注解處理器學(xué)習(xí)之編譯時處理的注解詳析
編譯時注解相信對每一個java開發(fā)者來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于Java注解處理器學(xué)習(xí)之編譯時處理的注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧2018-05-05json如何解析混合數(shù)組對象到實體類的list集合里去
這篇文章主要介紹了json解析混合數(shù)組對象到實體類的list集合里去的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06