JAVA自帶API的壓縮與解壓


Java API中的 java.util.zip.*;包下包含了Java對於壓縮文件的所有相關操作。我們可以使用該包中的方法,結合IO中的相關知識,進行文件的壓縮和解壓縮相關操作。

ZipFile

java中的每一個壓縮文件都是可以使用ZipFile來進行表示的。

    
    
   
   
           
  1. File file = new File( "F:/zippath.zip");
  2. ZipFile zipFile = new ZipFile(file);
  3. System.out.println( "壓縮文件的名稱為:" + zipFile.getName());

壓縮單個文件


    
    
   
   
           
  1. /** 壓縮單個文件*/
  2. public static void ZipFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);
  5. File zipFile = new File(zippath);
  6. InputStream input = new FileInputStream(file);
  7. ZipOutputStream zipOut = new ZipOutputStream( new FileOutputStream(zipFile));
  8. zipOut.putNextEntry( new ZipEntry(file.getName()));
  9. int temp = 0;
  10. while((temp = input.read()) != - 1){
  11. zipOut.write(temp);
  12. }
  13. input.close();
  14. zipOut.close();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }

應用:

ZipFile("d:/hello.txt", "d:/hello.zip");
   
   
  
  
          

壓縮多個文件(文件夾)


    
    
   
   
           
  1. /** 一次性壓縮多個文件,文件存放至一個文件夾中*/
  2. public static void ZipMultiFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath); // 要被壓縮的文件夾
  5. File zipFile = new File(zippath);
  6. InputStream input = null;
  7. ZipOutputStream zipOut = new ZipOutputStream( new FileOutputStream(zipFile));
  8. if(file.isDirectory()){
  9. File[] files = file.listFiles();
  10. for( int i = 0; i < files.length; ++i){
  11. input = new FileInputStream(files[i]);
  12. zipOut.putNextEntry( new ZipEntry(file.getName() + File.separator + files[i].getName()));
  13. int temp = 0;
  14. while((temp = input.read()) != - 1){
  15. zipOut.write(temp);
  16. }
  17. input.close();
  18. }
  19. }
  20. zipOut.close();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }

應用:

ZipMultiFile("f:/uu", "f:/zippath.zip");
   
   
  
  
          

解壓縮單個文件


    
    
   
   
           
  1. /** 解壓縮(解壓縮單個文件)*/
  2. public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
  3. try {
  4. File file = new File(zippath); //壓縮文件路徑和文件名
  5. File outFile = new File(outfilepath); //解壓后路徑和文件名
  6. ZipFile zipFile = new ZipFile(file);
  7. ZipEntry entry = zipFile.getEntry(filename); //所解壓的文件名
  8. InputStream input = zipFile.getInputStream(entry);
  9. OutputStream output = new FileOutputStream(outFile);
  10. int temp = 0;
  11. while((temp = input.read()) != - 1){
  12. output.write(temp);
  13. }
  14. input.close();
  15. output.close();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }

應用:

ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
   
   
  
  
          

 解壓縮多個文件

ZipInputStream類:
當我們需要解壓縮多個文件的時候,ZipEntry就無法使用了。
 如果想操作更加復雜的壓縮文件,我們就必須使用ZipInputStream類。

    
    
   
   
           
  1. /** 解壓縮(壓縮文件中包含多個文件)可代替上面的方法使用。
  2. * ZipInputStream類
  3. * 當我們需要解壓縮多個文件的時候,ZipEntry就無法使用了,
  4. * 如果想操作更加復雜的壓縮文件,我們就必須使用ZipInputStream類
  5. * */
  6. public static void ZipContraMultiFile(String zippath ,String outzippath){
  7. try {
  8. File file = new File(zippath);
  9. File outFile = null;
  10. ZipFile zipFile = new ZipFile(file);
  11. ZipInputStream zipInput = new ZipInputStream( new FileInputStream(file));
  12. ZipEntry entry = null;
  13. InputStream input = null;
  14. OutputStream output = null;
  15. while((entry = zipInput.getNextEntry()) != null){
  16. System.out.println( "解壓縮" + entry.getName() + "文件");
  17. outFile = new File(outzippath + File.separator + entry.getName());
  18. if(!outFile.getParentFile().exists()){
  19. outFile.getParentFile().mkdir();
  20. }
  21. if(!outFile.exists()){
  22. outFile.createNewFile();
  23. }
  24. input = zipFile.getInputStream(entry);
  25. output = new FileOutputStream(outFile);
  26. int temp = 0;
  27. while((temp = input.read()) != - 1){
  28. output.write(temp);
  29. }
  30. input.close();
  31. output.close();
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }

應用:


    
    
   
   
           
  1. ZipContraMultiFile( "f:/zippath.zip", "d:/");
  2. ZipContraMultiFile( "d:/hello.zip", "d:/");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM