Search in sources :

Example 1 with CircularByteBuffer

use of com.mucommander.commons.util.CircularByteBuffer in project mucommander by mucommander.

the class SevenZipArchiveFile method getEntryInputStream.

// ////////////////////////////////////////
// AbstractROArchiveFile implementation //
// ////////////////////////////////////////
@Override
public InputStream getEntryInputStream(final ArchiveEntry entry, ArchiveEntryIterator entryIterator) throws IOException, UnsupportedFileOperationException {
    final IInArchive sevenZipFile = openSevenZipFile();
    /*		ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

//		threadPool.execute(new Runnable() {
//			public void run() {
//		        BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
		        try {
		        	int arrays []  = new int[1];
		            for(int i = 0 ; i < sevenZipFile.size() ; i++) {
//		                	System.out.println("check " + sevenZipFile.getEntry(i).getName());
		                if (entry.getPath().equals(sevenZipFile.getEntry(i).getName())) {
		                	System.out.println("entry.getPath = " + entry.getPath() + ", sevenZipFile.getEntry(i).getName() " + sevenZipFile.getEntry(i).getName());
		                    arrays[0] = i;
		                    break;
		                }
		            }
		        	
					MuArchiveExtractCallback extractCallbackSpec = new MuArchiveExtractCallback(os);//, entry.getPath());
			        extractCallbackSpec.Init(sevenZipFile);
			        try {
			        	sevenZipFile.Extract(arrays, 1, IInArchive.NExtract_NAskMode_kExtract , extractCallbackSpec);
			        }
			        catch (Exception e) {
			        	e.printStackTrace();
//			        	return;
			        }
//			        sevenZipFile.Extract(null, -1, IInArchive.NExtract_NAskMode_kExtract , extractCallbackSpec);
			        try
			        {
			        Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
			        }
			        catch(InterruptedException e)
			        {
			        e.printStackTrace();
			        }
			        
			        System.out.println("stopped");
					
//			        bufferedOut.flush();
				}
                catch (Exception e) {
                    LOGGER.info("Error while retrieving 7zip entry {}", e);
                    System.out.println("Error while retrieving 7zip entry {}");
				}
                finally {
//                    try { bufferedOut.close(); }
//                    catch(IOException e) {
//                        // Not much we can do about it
//                    	e.printStackTrace();
//                    }

                    try { in.close(); }
                    catch(IOException e) {
                        // Not much we can do about it
                    	e.printStackTrace();
                    }

                    try { sevenZipFile.close(); }
                    catch(IOException e) {
                        // Not much we can do about it
                    	e.printStackTrace();
                    }
                }
//			}
//		});
		
		return new ByteArrayInputStream(os.toByteArray()); */
    final int[] arrays = new int[1];
    for (int i = 0; i < sevenZipFile.size(); i++) {
        // System.out.println("check " + sevenZipFile.getEntry(i).getName());
        if (entry.getPath().equals(sevenZipFile.getEntry(i).getName())) {
            arrays[0] = i;
            break;
        }
    }
    final CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
    new Thread(new Runnable() {

        public void run() {
            MuArchiveExtractCallback extractCallbackSpec = new MuArchiveExtractCallback(cbb.getOutputStream(), entry.getPath());
            extractCallbackSpec.Init(sevenZipFile);
            try {
                sevenZipFile.Extract(arrays, 1, IInArchive.NExtract_NAskMode_kExtract, extractCallbackSpec);
            } catch (Exception e) {
                e.printStackTrace();
            // return;
            }
        }
    }).start();
    return cbb.getInputStream();
}
Also used : IInArchive(com.mucommander.commons.file.archive.sevenzip.provider.SevenZip.Archive.IInArchive) CircularByteBuffer(com.mucommander.commons.util.CircularByteBuffer) IOException(java.io.IOException) UnsupportedFileOperationException(com.mucommander.commons.file.UnsupportedFileOperationException)

Example 2 with CircularByteBuffer

use of com.mucommander.commons.util.CircularByteBuffer in project mucommander by mucommander.

the class RarFile method getEntryInputStream.

public InputStream getEntryInputStream(String path) throws IOException, RarException {
    final FileHeader header = archive.getFileHeaders().stream().filter(h -> h.getFileNameString().equals(path)).findFirst().orElse(null);
    // part - don't extract it and throw corresponding exception to raise an error.
    if (header.isSplitAfter())
        throw new RarException(new Throwable("Splitting is not implemented yet"));
    final CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
    new Thread(new Runnable() {

        public void run() {
            try {
                archive.extractFile(header, cbb.getOutputStream());
            } catch (RarException e) {
                e.printStackTrace();
            } finally {
                try {
                    cbb.getOutputStream().close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }).start();
    return cbb.getInputStream();
}
Also used : RarException(com.github.junrar.exception.RarException) IOException(java.io.IOException) FileHeader(com.github.junrar.rarfile.FileHeader) CircularByteBuffer(com.mucommander.commons.util.CircularByteBuffer)

Aggregations

CircularByteBuffer (com.mucommander.commons.util.CircularByteBuffer)2 IOException (java.io.IOException)2 RarException (com.github.junrar.exception.RarException)1 FileHeader (com.github.junrar.rarfile.FileHeader)1 UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)1 IInArchive (com.mucommander.commons.file.archive.sevenzip.provider.SevenZip.Archive.IInArchive)1