2016-06-29 18 views
5

Ho bisogno di aprire un file compresed (zml, non ho potuto trovare informazioni su tale estensione) come 7zip lo fa con java.Decompressione di un file come 7zip con Java

Ho un file zml, se lo apro con 7zip mi chiede la password, poi inserisco la password e posso aprire il file.

Ho bisogno di fare lo stesso con java, qualcuno potrebbe darmi un consiglio per questo?

Cordiali saluti.

Juan

+2

ciao. Penso che i binding Java 7zip siano probabilmente quello che stai cercando: http://sevenzipjbind.sourceforge.net/index.html – trooper

+0

Grazie, darò un'occhiata. –

+0

Ho trovato con le proprietà del file 7zip che il file era un file zip, quindi uso Zip4j. Potrei ingannare il file seguendo le indicazioni di questo post: https://stackoverflow.com/questions/11174851/how-to-use-zip4j-to-extract-an-zip-file-with-password-protection Grazie a tutti per il tuo aiuto. –

risposta

3

Sulla base di commento @trooper, sono stato in grado di estrarre un file .7z che è stato protetto da password. Prova il seguente codice. Sarà necessario impostare il classpath con 7-Zip-JBinding (http://sevenzipjbind.sourceforge.net/index.html). Questo codice è una versione modificata di frammenti di codice trovato alla 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

Se cercate una soluzione Java puro, è possibile utilizzare Apache Commons Compress, che supporta anche la lettura dei file crittografati.

Problemi correlati