Advertisement
Guest User

Untitled

a guest
Mar 29th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. package org.jdownloader.extensions.extraction.multi;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.RandomAccessFile;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9. import net.sf.sevenzipjbinding.ArchiveFormat;
  10. import net.sf.sevenzipjbinding.IArchiveOpenCallback;
  11. import net.sf.sevenzipjbinding.IArchiveOpenVolumeCallback;
  12. import net.sf.sevenzipjbinding.ICryptoGetTextPassword;
  13. import net.sf.sevenzipjbinding.IInStream;
  14. import net.sf.sevenzipjbinding.ISevenZipInArchive;
  15. import net.sf.sevenzipjbinding.PropID;
  16. import net.sf.sevenzipjbinding.SevenZip;
  17. import net.sf.sevenzipjbinding.SevenZipException;
  18. import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
  19.  
  20. public class OpenMultipartArchiveRar {
  21.     private static class ArchiveOpenVolumeCallback implements IArchiveOpenVolumeCallback, IArchiveOpenCallback, ICryptoGetTextPassword {
  22.  
  23.         /**
  24.          * Cache for opened file streams
  25.          */
  26.         private Map<String, RandomAccessFile> openedRandomAccessFileList = new HashMap<String, RandomAccessFile>();
  27.  
  28.         /**
  29.          * Name of the last volume returned by {@link #getStream(String)}
  30.          */
  31.         private String                        name;
  32.  
  33.         /**
  34.          * This method should at least provide the name of the last opened
  35.          * volume (propID=PropID.NAME).
  36.          *
  37.          * @see IArchiveOpenVolumeCallback#getProperty(PropID)
  38.          */
  39.         public Object getProperty(PropID propID) throws SevenZipException {
  40.             switch (propID) {
  41.             case NAME:
  42.                 return name;
  43.             }
  44.             return null;
  45.         }
  46.  
  47.         /**
  48.          * The name of the required volume will be calculated out of the name of
  49.          * the first volume and a volume index. In case of RAR file, the
  50.          * substring ".partNN." in the name of the volume file will indicate a
  51.          * volume with id NN. For example:
  52.          * <ul>
  53.          * <li>test.rar - single part archive or multi-part archive with a
  54.          * single volume</li>
  55.          * <li>test.part23.rar - 23-th part of a multi-part archive</li>
  56.          * <li>test.part001.rar - first part of a multi-part archive. "00"
  57.          * indicates, that at least 100 volumes must exist.</li>
  58.          * </ul>
  59.          */
  60.         public IInStream getStream(String filename) throws SevenZipException {
  61.             try {
  62.                 // We use caching of opened streams, so check cache first
  63.                 RandomAccessFile randomAccessFile = openedRandomAccessFileList.get(filename);
  64.                 if (randomAccessFile != null) { // Cache hit.
  65.                     // Move the file pointer back to the beginning
  66.                     // in order to emulating new stream
  67.                     randomAccessFile.seek(0);
  68.  
  69.                     // Save current volume name in case getProperty() will be
  70.                     // called
  71.                     name = filename;
  72.  
  73.                     return new RandomAccessFileInStream(randomAccessFile);
  74.                 }
  75.  
  76.                 // Nothing useful in cache. Open required volume.
  77.                 randomAccessFile = new RandomAccessFile(filename, "r");
  78.  
  79.                 // Put new stream in the cache
  80.                 openedRandomAccessFileList.put(filename, randomAccessFile);
  81.  
  82.                 // Save current volume name in case getProperty() will be called
  83.                 name = filename;
  84.                 return new RandomAccessFileInStream(randomAccessFile);
  85.             } catch (FileNotFoundException fileNotFoundException) {
  86.                 // Required volume doesn't exist. This happens if the volume:
  87.                 // 1. never exists. 7-Zip doesn't know how many volumes should
  88.                 // exist, so it have to try each volume.
  89.                 // 2. should be there, but doesn't. This is an error case.
  90.  
  91.                 // Since normal and error cases are possible,
  92.                 // we can't throw an error message
  93.                 return null; // We return always null in this case
  94.             } catch (Exception e) {
  95.                 throw new RuntimeException(e);
  96.             }
  97.         }
  98.  
  99.         /**
  100.          * Close all opened streams
  101.          */
  102.         void close() throws IOException {
  103.             for (RandomAccessFile file : openedRandomAccessFileList.values()) {
  104.                 file.close();
  105.             }
  106.         }
  107.  
  108.         public void setCompleted(Long files, Long bytes) throws SevenZipException {
  109.         }
  110.  
  111.         public void setTotal(Long files, Long bytes) throws SevenZipException {
  112.         }
  113.  
  114.         @Override
  115.         public String cryptoGetTextPassword() throws SevenZipException {
  116.             return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  117.         }
  118.     }
  119.  
  120.     public static void main(String[] args) {
  121.  
  122.         ArchiveOpenVolumeCallback archiveOpenVolumeCallback = null;
  123.         ISevenZipInArchive inArchive = null;
  124.         try {
  125.  
  126.             archiveOpenVolumeCallback = new ArchiveOpenVolumeCallback();
  127.             IInStream inStream = archiveOpenVolumeCallback.getStream("C:/Users/Thomas/Downloads/MyMultiPart.part1.rar");
  128.             inArchive = SevenZip.openInArchive(ArchiveFormat.RAR, inStream, archiveOpenVolumeCallback);
  129.  
  130.             System.out.println("   Size   | Compr.Sz. | Filename");
  131.             System.out.println("----------+-----------+---------");
  132.             int itemCount = inArchive.getNumberOfItems();
  133.             for (int i = 0; i < itemCount; i++) {
  134.                 System.out.println(String.format("%9s | %9s | %s", inArchive.getProperty(i, PropID.SIZE), inArchive.getProperty(i, PropID.PACKED_SIZE), inArchive.getProperty(i, PropID.PATH)));
  135.             }
  136.         } catch (Exception e) {
  137.             System.err.println("Error occurs: " + e);
  138.             System.exit(1);
  139.         } finally {
  140.             if (inArchive != null) {
  141.                 try {
  142.                     inArchive.close();
  143.                 } catch (SevenZipException e) {
  144.                     System.err.println("Error closing archive: " + e);
  145.                 }
  146.             }
  147.             if (archiveOpenVolumeCallback != null) {
  148.                 try {
  149.                     archiveOpenVolumeCallback.close();
  150.                 } catch (IOException e) {
  151.                     System.err.println("Error closing file: " + e);
  152.                 }
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement