2017-07-08 49 views
0

我對Java很新,我正在嘗試編寫一個java程序來解壓所有具有相同密碼的目錄中的所有zip文件。 我已經寫了一個程序,通過給出密碼解壓縮一個特定的zip文件。如何使用java在一個具有相同密碼的目錄中解壓縮多個受pssword保護的zip文件?

import java.io.OutputStream; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.PathMatcher; 
import java.nio.file.Paths; 
import java.util.List; 
import java.util.stream.Stream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

//import java.util.zip.ZipFile; 
import net.lingala.zip4j.core.ZipFile; 
import net.lingala.zip4j.model.FileHeader; 

public class Extraction { 
    public Extraction() { 

    try { 

    ZipFile zipFile = new 
    ZipFile("C:\\Users\\Desktop\\ZipFile\\myzip.zip"); 

    if (zipFile.isEncrypted()) { 

    zipFile.setPassword("MYPASS!"); 
    } 

    List fileHeaderList = zipFile.getFileHeaders(); 

    for (int i = 0; i < fileHeaderList.size(); i++) { 
    FileHeader fileHeader = (FileHeader) fileHeaderList.get(i); 

    zipFile.extractFile(fileHeader, "C:\\Users\\Desktop\\ZipFile"); 
    System.out.println("Extracted"); 
    } 

    } catch (Exception e) { 
    System.out.println("Please Try Again"); 
    } 

    } 
     public static void main(String[] args) { 
      new Extraction(); 

     } 
} 

我也寫了一個代碼,它將所有zip文件(其中沒有密碼保護)解壓縮到一個文件夾中。

import java.io.OutputStream; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.PathMatcher; 
import java.nio.file.Paths; 
import java.util.List; 
import java.util.stream.Stream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

//import java.util.zip.ZipFile; 
import net.lingala.zip4j.core.ZipFile; 
import net.lingala.zip4j.model.FileHeader; 

public class Extraction { 


    public static void main(String[] args) { 
     Extraction unzipper = new Extraction(); 
     unzipper.unzipZipsInDirTo(Paths.get("D:/"), Paths.get("D:/unzipped")); 

    } 

    public void unzipZipsInDirTo(Path searchDir, Path unzipTo) { 

     final PathMatcher matcher = searchDir.getFileSystem().getPathMatcher("glob:**/*.zip"); 
     try (final Stream<Path> stream = Files.list(searchDir)) { 
      stream.filter(matcher::matches).forEach(zipFile -> unzip(zipFile, unzipTo)); 
     } catch (Exception e) { 
      System.out.println("Something went wrong, Please try again!!"); 
     } 
    } 

    public void unzip(Path zipFile, Path outputPath) { 
     try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile))) { 

      ZipEntry entry = zis.getNextEntry(); 

      while (entry != null) { 

       Path newFilePath = outputPath.resolve(entry.getName()); 
       if (entry.isDirectory()) { 
        Files.createDirectories(newFilePath); 
       } else { 
        if (!Files.exists(newFilePath.getParent())) { 
         Files.createDirectories(newFilePath.getParent()); 
        } 
        try (OutputStream bos = Files.newOutputStream(outputPath.resolve(newFilePath))) { 
         byte[] buffer = new byte[Math.toIntExact(entry.getSize())]; 

         int location; 

         while ((location = zis.read(buffer)) != -1) { 
          bos.write(buffer, 0, location); 
         } 
        } 
       } 
       entry = zis.getNextEntry(); 
      } 
     } catch (Exception e1) { 
      System.out.println("Please try again"); 
     } 
    } 

} 

我有點困惑如何整合這些編碼解碼,使該解壓縮在有密碼保護的目錄下的所有zip文件,並且具有相同密碼的程序。 你們中的任何人都可以幫助我整合這兩個代碼。 我搜索了很多,但沒有得到正確的決議,所以我張貼這個。 我希望我的問題很清楚。

回答

0

謝謝你們,儘管沒有人回答我的問題。我找到了我發佈的答案,因爲可能有其他人可能正在尋找類似的答案。

import java.io.File; 
import java.util.List; 

import javax.swing.filechooser.FileNameExtensionFilter; 

import net.lingala.zip4j.core.ZipFile; 
import net.lingala.zip4j.model.FileHeader; 

public class SamExtraction { 

    public static void main(String[] args) { 

     final FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("N/A", "zip"); 
     //Folder where zip file is present 
     final File file = new File("C:/Users/Desktop/ZipFile"); 
     for (final File child : file.listFiles()) { 
      try { 
       ZipFile zipFile = new ZipFile(child); 
       if (extensionFilter.accept(child)) { 
        if (zipFile.isEncrypted()) { 
         //Your ZIP password 
         zipFile.setPassword("MYPASS!"); 
        } 
        List fileHeaderList = zipFile.getFileHeaders(); 

        for (int i = 0; i < fileHeaderList.size(); i++) { 
         FileHeader fileHeader = (FileHeader) fileHeaderList.get(i); 
         //Path where you want to Extract 
         zipFile.extractFile(fileHeader, "C:/Users/Desktop/ZipFile"); 
         System.out.println("Extracted"); 
        } 
       } 
      } catch (Exception e) { 
       System.out.println("Please Try Again"); 
      } 
     } 

    } 
} 
相關問題