java - 如何使用 InputStream 从 ZIP 中读取文件?

标签 java zip inputstream

我必须使用 SFTP 从 ZIP 存档中获取文件内容(只有一个文件,我知道它的名称)。我唯一拥有的是 ZIP 的 InputStream。大多数示例显示了如何使用此语句获取内容:

ZipFile zipFile = new ZipFile("location");

但正如我所说,我的本地计算机上没有 ZIP 文件,我不想下载它。 InputStream 是否足以读取?

UPD:我就是这样做的:

import java.util.zip.ZipInputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SFTP {


    public static void main(String[] args) {

        String SFTPHOST = "host";
        int SFTPPORT = 3232;
        String SFTPUSER = "user";
        String SFTPPASS = "mypass";
        String SFTPWORKINGDIR = "/dir/work";
        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
            session.setPassword(SFTPPASS);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp) channel;
            channelSftp.cd(SFTPWORKINGDIR);
            ZipInputStream stream = new ZipInputStream(channelSftp.get("file.zip"));
            ZipEntry entry = zipStream.getNextEntry();
            System.out.println(entry.getName); //Yes, I got its name, now I need to get content
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            session.disconnect();
            channelSftp.disconnect();
            channel.disconnect();
        }


    }
}

最佳答案

下面是一个关于如何提取 ZIP 文件的简单示例,您需要检查文件是否为目录。但这是最简单的。

您缺少的步骤是读取输入流并将内容写入缓冲区,然后再写入输出流。

// Expands the zip file passed as argument 1, into the
// directory provided in argument 2
public static void main(String args[]) throws Exception
{
    if(args.length != 2)
    {
        System.err.println("zipreader zipfile outputdir");
        return;
    }

    // create a buffer to improve copy performance later.
    byte[] buffer = new byte[2048];

    // open the zip file stream
    InputStream theFile = new FileInputStream(args[0]);
    ZipInputStream stream = new ZipInputStream(theFile);
    String outdir = args[1];

    try
    {

        // now iterate through each item in the stream. The get next
        // entry call will return a ZipEntry for each file in the
        // stream
        ZipEntry entry;
        while((entry = stream.getNextEntry())!=null)
        {
            String s = String.format("Entry: %s len %d added %TD",
                            entry.getName(), entry.getSize(),
                            new Date(entry.getTime()));
            System.out.println(s);

            // Once we get the entry from the stream, the stream is
            // positioned read to read the raw data, and we keep
            // reading until read returns 0 or less.
            String outpath = outdir + "/" + entry.getName();
            FileOutputStream output = null;
            try
            {
                output = new FileOutputStream(outpath);
                int len = 0;
                while ((len = stream.read(buffer)) > 0)
                {
                    output.write(buffer, 0, len);
                }
            }
            finally
            {
                // we must always close the output file
                if(output!=null) output.close();
            }
        }
    }
    finally
    {
        // we must always close the zip file.
        stream.close();
    }
}

代码摘自以下网站:

http://www.thecoderscorner.com/team-blog/java-and-jvm/12-reading-a-zip-file-from-java-using-zipinputstream#.U4RAxYamixR

关于java - 如何使用 InputStream 从 ZIP 中读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23869228/

相关文章:

java - 有没有更好的方法来测试 JTree 节点扩展与否?

linux - tar zxf 与 -xvf 之间的区别

java - 如何使用 ZipEntry 读取位于另一个 zip 文件中的一个 zip 文件中的数据?

java - InputStream 的问题

java - 如何在 System.in 上使用多个 Scanner 对象?

java - AbstractMap 重写 hashcode 存在问题

java - Spring Boot - javax Import 语句无法正常工作

java - 在 Java 中使用 for 循环创建按钮

php - PHP 中的压缩流

java - 从 InputStream 读取并附加到 String