1

我查看了 at4j 和 7-Zip-JBinding (他们的 javadoc 和文档),但他们似乎无法在不提取的情况下读取(并从存档文件中获取 InputStream)

有没有我遗漏或找不到的方法?

除了提取到临时文件夹来阅读它之外的解决方案

我期待在 at4j 或 7-Zip-JBinding 中如何做到这一点的答案

换句话说,我想知道如何在 at4j 或 7-Zip-JBinding 中使用下面提到的函数

我知道 java 的内置有 getInputStream 我目前正在以这种方式使用它

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/**
 * get input stream of current file
 * @param path path inside zip
 * @return InputStream
 */
public InputStream getInputStream(String path){
    try {
        ZipEntry entry = zipFile.getEntry(path);
        if(entry!=null){
            return zipFile.getInputStream(entry);
        }
        return new ByteArrayInputStream("Not Found".getBytes());
    } catch (Exception ex) {
        //handle exception
    }

    return null;
}

(^^ zipFile 是一个 ZipFile 对象)

我想要的是

4

2 回答 2

5

使用 7-Zip-JBinding 找到了解决方案

只需要使用 ByteArrayInputStream ,到目前为止这适用于一个小文件

将存档作为参数传递以获取打印的所有文件

文件 ExtractItemsSimple.java

import java.io.IOException;
import java.io.RandomAccessFile;
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 ExtractItemsSimple {
    public static void main(String[] args) {
        RandomAccessFile randomAccessFile = null;
        ISevenZipInArchive inArchive = null;
        try {
            randomAccessFile = new RandomAccessFile(args[0], "r");
            inArchive = SevenZip.openInArchive(null, // autodetect archive type
                    new RandomAccessFileInStream(randomAccessFile));

            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

            for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                final int[] hash = new int[] { 0 };
                if (!item.isFolder()) {
                    System.out.println(ArchieveInputStreamHandler.slurp(new ArchieveInputStreamHandler(item).getInputStream(),1000));
                }
            }
        } 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);
                }
            }
        }
    }
}

文件 ArchieveInputStreamHandler.java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;



public class ArchieveInputStreamHandler {

    private ISimpleInArchiveItem item;
    private ByteArrayInputStream arrayInputStream;
    public ArchieveInputStreamHandler(ISimpleInArchiveItem item) {
        this.item = item;
    }

    public InputStream getInputStream() throws SevenZipException{

        item.extractSlow(new ISequentialOutStream() {
                        @Override
                        public int write(byte[] data) throws SevenZipException {
                            arrayInputStream = new ByteArrayInputStream(data);
                            return data.length; // Return amount of consumed data
                        }
                    });
        return arrayInputStream;
    }
    //got from http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
    public static String slurp(final InputStream is, final int bufferSize){
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        try {
            final Reader in = new InputStreamReader(is, "UTF-8");
            try {
              for (;;) {
                int rsz = in.read(buffer, 0, buffer.length);
                if (rsz < 0)
                  break;
                out.append(buffer, 0, rsz);
              }
            }
            finally {
              in.close();
            }
        }
        catch (UnsupportedEncodingException ex) {
        /* ... */
        }
        catch (IOException ex) {
          /* ... */
        }
        return out.toString();
    }
}
于 2013-08-03T17:10:04.797 回答
0

您是否正在寻找http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipInputStream.html可以提取 zip 文件中的条目而无需完全提取它。

于 2013-08-03T15:47:17.537 回答