2016-06-29 111 views
5

我需要打開一個壓縮文件(zml,我無法找到關於該擴展的信息),就像7zip使用java一樣。使用Java解壓縮像7zip這樣的文件

我有一個zml文件,如果我打開它與7zip它問我的密碼,然後我把密碼,可以打開文件。

我需要用java來做同樣的事情,任何人都可以給我一個建議嗎?

此致敬禮。

胡安

+2

嗨。我認爲7zip java綁定可能是你正在尋找的:http://sevenzipjbind.sourceforge.net/index.html – trooper

+0

謝謝你,我要去看看。 –

+0

我發現與7zip的文件屬性,該文件是一個zip文件,所以我使用Zip4j。我可以按照這篇文章的說明去壓縮文件:https://stackoverflow.com/questions/11174851/how-to-use-zip4j-to-extract-an-zip-file-with-password-protection謝謝大家您的幫助。 –

回答

3

基於@trooper評論,我能提取被密碼保護的文件.7z壓縮。嘗試下面的代碼。您將需要使用7-Zip-JBinding(http://sevenzipjbind.sourceforge.net/index.html)設置類路徑。此代碼的代碼片段修改後的版本在http://sevenzipjbind.sourceforge.net/extraction_snippets.html

import java.io.FileNotFoundException; 
import java.io.RandomAccessFile; 
import java.util.Arrays; 

import net.sf.sevenzipjbinding.ExtractOperationResult; 
import net.sf.sevenzipjbinding.IInArchive; 
import net.sf.sevenzipjbinding.ISequentialOutStream; 
import net.sf.sevenzipjbinding.SevenZip; 
import net.sf.sevenzipjbinding.SevenZipException; 
import net.sf.sevenzipjbinding.SevenZipNativeInitializationException; 
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; 
import net.sf.sevenzipjbinding.simple.ISimpleInArchive; 
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; 

public class Extract { 
    public static void main(String[] args) throws SevenZipException, FileNotFoundException { 
     try { 
      SevenZip.initSevenZipFromPlatformJAR(); 
      System.out.println("7-Zip-JBinding library was initialized"); 
      RandomAccessFile randomAccessFile = new RandomAccessFile("YOUR FILE NAME", "r"); 

      IInArchive inArchive = SevenZip.openInArchive(null, // Choose format 
                   // automatically 
        new RandomAccessFileInStream(randomAccessFile)); 
      System.out.println(inArchive.getNumberOfItems()); 

      // Getting simple interface of the archive inArchive 
      ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface(); 

      System.out.println(" Hash | Size | Filename"); 
      System.out.println("----------+------------+---------"); 

      for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { 
       final int[] hash = new int[] { 0 }; 
       if (!item.isFolder()) { 
        ExtractOperationResult result; 

        final long[] sizeArray = new long[1]; 
        result = item.extractSlow(new ISequentialOutStream() { 
         public int write(byte[] data) throws SevenZipException { 
          hash[0] ^= Arrays.hashCode(data); // Consume data 
          for (byte b : data) { 
           System.out.println((char) b); 
          } 
          sizeArray[0] += data.length; 
          return data.length; // Return amount of consumed 
               // data 
         } 
        }, "YOUR PASSWORD HERE"); 

        if (result == ExtractOperationResult.OK) { 
         System.out.println(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath())); 
        } else { 
         System.err.println("Error extracting item: " + result); 
        } 
       } 
      } 

     } catch (SevenZipNativeInitializationException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
3

如果你尋找一個純Java的解決方案,你可以使用Apache Commons Compress,同時也支持讀取加密文件中找到。

相關問題