I accept the zip directory from the server with no problems, but the next step I need to unpack it I do it using this method

 Decompress d = new Decompress(avatarModelZipFile.toString(), getAvatarPath(context).toString()); d.unzip(); 

here's a class

 public class Decompress { private String _zipFile; private String _location; public Decompress(String zipFile, String location) { _zipFile = zipFile; _location = location; _dirChecker(""); } public void unzip() { try { FileInputStream fin = new FileInputStream(_zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { Log.v("Decompress", "Unzipping " + ze.getName()); if(ze.isDirectory()) { _dirChecker(ze.getName()); } else { FileOutputStream fos = new FileOutputStream(_location + ze.getName()); for (int c = zin.read(); c != -1; c = zin.read()) { ----> Log.e("!!!!!!!!!!!!!!!!!!!!!", "!!!!!!!!!!!!!!!!!"); fos.write(c); } zin.closeEntry(); fos.close(); } } zin.close(); } catch(Exception e) { Log.e("Decompress", "unzip", e); } } private void _dirChecker(String dir) { File f = new File(_location + dir); if(!f.isDirectory()) { f.mkdirs(); } } } 

and when execution reaches the cycle

 for (int c = zin.read(); c != -1; c = zin.read()) 

then it does not go out of it ... I checked a few other examples and they also implemented zip unpacking in the same way ... Why then my cycle never gets the value c != -1; ?

What am I doing wrong?

One attempt to implement

 private void fff(String path) { Log.e("!!!!!!!!!!", "!!!!!!!!!!!! 1"); 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 { Log.e("!!!!!!!!!!", "!!!!!!!!!!!! 2"); 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 zipStream = new ZipInputStream(channelSftp.get(path)); zipStream.getNextEntry(); Scanner sc = new Scanner(zipStream); while (sc.hasNextLine()) { Log.e("!!!!!!!!!!", "!!!!!!!!!!!! 3"); System.out.println(sc.nextLine()); } } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { session.disconnect(); } if (channelSftp != null) { channelSftp.disconnect(); } if (channel != null) { channel.disconnect(); } } } 

But I get this error

[CDS] [DNS] Unable to resolve host "host": No address associated with hostname 06-25 14: 10: 28.217 17128-17608 / com.example.android.camera2basic.demo W / System.err: com.jcraft. jsch.JSchException: java.net.UnknownHostException: Unable to resolve host "host": No address associated with hostname 06-25 14: 10: 28.219 17128-17608 / com.example.android.camera2basic.demo W / System.err: at com.jcraft.jsch.Util.createSocket (Util.java upon49) 06-25 14: 10: 28.219 17128-17608 / com.example.android.camera2basic.demo W / System.err: at com.jcraft.jsch .Session.connect (Session.java:215) 06-25 14: 10: 28.219 17128-17608 / com.example.android.camera2basic.demo W / System.err: at com.jcraft.jsch.Session.connect (Session .java: 183) 06-25 14: 10: 28.219 17128-17608 / com.example.android.camera2basic.demo W / System.err: at com.example.android.camera2basic.tools.serverConnection.GetAvatar.fff (GetAvatar .java: 248) 06-25 14: 10: 28.219 17128-17608 / com.example.android.camera2basic.demo W / System.err: at com.example.android.camera2basic.tools.ser verConnection.GetAvatar.doInBackground (GetAvatar.java:53) 06-25 14: 10: 28.219 17128-17608 / com.example.android.camera2basic.demo W / System.err: at com.example.android.camera2basic.tools. serverConnection.GetAvatar.doInBackground (GetAvatar.java:29)

  • Try this: tyk - YuriySPb
  • @YuriiSPb Something for me is too confusing implementation ... I added a dependency, took a method like in the example and replaced a couple of lines as shown in the answer to that question, but I have an error and I don’t quite understand what needs to be done .. And of course if you describe in a few words how it works it would be cool)) I understand that it works as a connection to the server through a socket. Now by the way I will add a question - Aleksey Timoshchenko
  • 2
    brr, mixed up in a bunch of horsemen, jsch, scanner ... Maybe it’s just byte-bye reading a file with output to the log for each byte takes too much time. Try to stream the stream through the buffer (in the same way as you download a file from the Internet) - zRrr
  • @zRrr yes you are right, I just just added a working version to the answers ... But damn it I did it by typing, I remembered that I have a method that makes zip and in its likeness made a method that unfolds it ... How do you manage to determine the right solution?)) For the 2nd time you advise me with an accuracy of 100%) - Aleksey Timoshchenko

1 answer 1

In the end, that's how it all worked for me

 public class Decompress { private String _zipFile; private String location; public Decompress(String zipFile, String location) { _zipFile = zipFile; this.location = location; _dirChecker(""); } public void unzip() { try { FileInputStream fin = new FileInputStream(_zipFile); ZipInputStream zis = new ZipInputStream(fin); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { Log.e("Decompress", "Unzipping " + ze.getName()); if(ze.isDirectory()) { _dirChecker(ze.getName()); } else { write(zis, new FileOutputStream(location + ze.getName())); zis.closeEntry(); } } zis.close(); } catch(Exception e) { Log.e("Decompress", "unzip", e); } } private void write(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len); } out.close(); } private void _dirChecker(String dir) { File f = new File(location + dir); if(!f.isDirectory()) { f.mkdirs(); } } }