1

I am trying to unzip a file on SFTP server.

Vector filelist = channelSftp.ls("ML_E_DAM_D_op_files_*.zip");

StringBuilder result = new StringBuilder();
for (int i=0; i<filelist.size();i++)
{
    LsEntry entry = (LsEntry) filelist.get(i);
    System.out.println(entry.getFilename());                                
    
    byte[] buffer = new byte[2048];
    int len = 0;
    ZipInputStream stream1 = new ZipInputStream(channelSftp.get(entry.getFilename()));
    ZipEntry zEntry;                                                               
    while ((zEntry = stream1.getNextEntry()) != null) 
    {
        OutputStream out = channelSftp.put(zEntry.getName());
         
        System.out.println("here");
        while ((len = stream1.read(buffer)) != -1) 
        {
            out.write(buffer, 0, len);
        }
        System.out.println("here");
        out.close();
        stream1.closeEntry();                                                                                                                     
    }

The files gets created. But, all the contents are not copied. I receive this error while testing the lambda function.

java.io.IOException: error: 4: RequestQueue: unknown request id 239
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1433)
at java.base/java.io.FilterInputStream.read(Unknown Source)
at java.base/java.io.PushbackInputStream.read(Unknown Source)
at java.base/java.util.zip.InflaterInputStream.fill(Unknown Source)
at java.base/java.util.zip.InflaterInputStream.read(Unknown Source)
at java.base/java.util.zip.ZipInputStream.read(Unknown Source)
at java.base/java.io.FilterInputStream.read(Unknown Source)
at 

Can you please help me in unzipping the file on SFTP server? Is there any another way to do this?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Shatakshi
  • 11
  • 3

1 Answers1

0

While theoretically, you might have multiple files opened over one SFTP channel, it seems that JSch does not support this. Related question:
Multiple file downloads from SFTP using JSch failing with "Request queue: Unknown request issue" It's about use from multiple threads, but based on the problems you are facing, it seems that the JSch has problems with this even when using single thread.

Try opening one channel for get opearation and another for the put operations.

ChannelSftp getChannel = (ChannelSftp)session.openChannel("sftp");
getChannel.connect();
ChannelSftp putChannel = (ChannelSftp)session.openChannel("sftp");
putChannel.connect();
// ...
ZipInputStream stream1 = new ZipInputStream(getChannel.get(entry.getFilename()));
// ...
OutputStream out = putChannel.put(zEntry.getName());

Btw, are you aware that your code is not really "unzipping the file on SFTP server". It downloads the ZIP file, unzips it locally and uploads the individual files back.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992