I must get file content from ZIP archive (only one file, I know its name) using SFTP. The only thing I'm having is ZIP's InputStream
. Most examples show how get content using this statement:
ZipFile zipFile = new ZipFile("location");
But as I said, I don't have ZIP file on my local machine and I don't want to download it. Is an InputStream
enough to read?
UPD: This is how I do:
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(); } } }
ZipInputStream. read(byte[] buf, int off, int len) method reads from the current ZIP entry into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.
Methods. getComment(): String – returns the zip file comment, or null if none. getEntry(String name): ZipEntry – returns the zip file entry for the specified name, or null if not found. getInputStream(ZipEntry entry) : InputStream – Returns an input stream for reading the contents of the specified zip file entry.
public class ZipInputStream extends InflaterInputStream. This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.
To read the file represented by a ZipEntry you can obtain an InputStream from the ZipFile like this: ZipEntry zipEntry = zipFile. getEntry("dir/subdir/file1.
Below is a simple example on how to extract a ZIP File, you will need to check if the file is a directory. But this is the simplest.
The step you are missing is reading the input stream and writing the contents to a buffer which is written to an output stream.
// 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(); } }
Code excerpt came from the following site:
http://www.thecoderscorner.com/team-blog/java-and-jvm/12-reading-a-zip-file-from-java-using-zipinputstream#.U4RAxYamixR
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With