【问题标题】:how to use At4J or 7-Zip-JBinding to get an InputStream of a file?如何使用 At4J 或 7-Zip-JBinding 获取文件的 InputStream?
【发布时间】:2013-08-04 18:19:13
【问题描述】:

我查看了 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 对象)

【问题讨论】:

标签: java inputstream


【解决方案1】:

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

【讨论】:

  • 使用它我想要一个支持多种存档格式的文件
  • rar不能用
【解决方案2】:

使用 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();
    }
}

【讨论】:

  • 我正在尝试修改您的 slurp 方法以改为返回 InputStream。我试图返回这个 org.apache.commons.io.IOUtils.toInputStream(out.toString(),"UTF-8");但是它没有按预期工作,当我调试时,我看到输出字符串已经从文件中间解码了一个块。有什么想法吗?
  • 它只将第一笔数据写入私有 ByteArrayInputStream arrayInputStream;您应该使用 outStrem.write(data) 写入 OutPutStream
猜你喜欢
  • 2016-01-12
  • 2014-07-15
  • 2012-10-25
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多