2013-10-16 78 views
3

有人可以建議(舉例)任何適當和可以理解的方式如何提取基於InputStream的.7z擴展名的文件或文件。我已經檢查了Java API的XZ,但無法成功。等待任何建議。在java中解壓縮帶.7z擴展名的文件

+0

拿這個鏈接看看: http://stackoverflow.com/questions/12781207/compressing-decompressing-7z-file-in-java – AlexR

+0

你能使用調用7zip的自身的批處理文件? – Brian

回答

4

此代碼可能會對您有所幫助。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.util.Arrays; 

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

public class unzip { 
    public static void main(String[] args) { 

     RandomAccessFile randomAccessFile = null; 
     ISevenZipInArchive inArchive = null; 

     try { 
      randomAccessFile = new RandomAccessFile("oclHashcat-plus-0.14.7z", "r"); 
      inArchive = SevenZip.openInArchive(null, // autodetect archive type 
        new RandomAccessFileInStream(randomAccessFile)); 

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

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

      for (final 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 { 

          //Write to file 
          FileOutputStream fos; 
          try { 
           File file = new File(item.getPath()); 
           file.getParentFile().mkdirs(); 
           fos = new FileOutputStream(file); 
           fos.write(data); 
           fos.close(); 

          } catch (FileNotFoundException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          hash[0] ^= Arrays.hashCode(data); // Consume data 
          sizeArray[0] += data.length; 
          return data.length; // Return amount of consumed data 
         } 
        }); 
        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 (Exception e) { 
      System.err.println("Error occurs: " + e); 
      System.exit(1); 
     } finally { 
      if (inArchive != null) { 
       try { 
        inArchive.close(); 
       } catch (SevenZipException e) { 
        System.err.println("Error closing archive: " + e); 
       } 
      } 
      if (randomAccessFile != null) { 
       try { 
        randomAccessFile.close(); 
       } catch (IOException e) { 
        System.err.println("Error closing file: " + e); 
       } 
      } 
     } 
    } 
} 
+0

我明白了,但需要更多的建議 - 如何將這個罐子添加到maven項目? mvnrepo不提供任何依賴 – user1421496

+0

謝謝,黑暗騎士。回答我的第二個問題 - http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them/7623805#7623805,最有用的是python腳本 – user1421496