【发布时间】:2018-09-12 04:20:18
【问题描述】:
我基本上是在尝试创建一个解压缩函数。我在下面的块中调用了带有参数的函数:
UnzipUtility unzipUtility = new UnzipUtility();
try {
unzipUtility.unzip(localFilePath, parentPath);
} catch (IOException e) {
e.printStackTrace();
}
该方法的定义是在一个类UnzipUtility中,代码如下:
public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
但是在运行时,虽然参数在主类中正确传递,但是在 unzip 方法中值显示为 null。
请帮忙解决一下
主要类如下:
package com.example.sftpconnection;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
//import java.io.BufferedOutputStream;
import java.io.File;
//import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.OutputStream;
import java.util.List;
import com.example.sftpconnection.UnzipUtility;
public class SFTPActivity extends AppCompatActivity {
private String fileName = "1234.zip";
private String localFilePath;
private String parentPath;
@SuppressLint("StaticFieldLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sftp);
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... params) {
try {
Downloader(fileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
UnzipUtility unzipUtility = new UnzipUtility();
try {
unzipUtility.unzip(localFilePath, parentPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public void Downloader(String fileName) {
String user = "1234";
String pass = "1234";
String host = "1234";
int portNum = 22;
JSch jsch = new JSch();
Session session;
try {
session = jsch.getSession(user,host,portNum);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
File localFile = File.createTempFile("1234",".zip");
sftpChannel.get(fileName,localFile.getAbsolutePath());
//sftpChannel.get(fileName);
Log.d(fileName, " has been downloaded");
sftpChannel.exit();
session.disconnect();
localFilePath = localFile.getAbsolutePath();
parentPath = localFile.getParent();
} catch (JSchException | IOException | SftpException e) {
e.printStackTrace();
}
}
}
出于安全原因,我编辑了数据字段。
【问题讨论】:
-
你在哪里得到 null ?
-
发布错误日志
-
能介绍一下主要课程吗?
-
你说的不可能。你肯定传递了一些错误的东西。尝试调试。
-
@sasikumar 在调试过程中,我在解压缩方法定义中得到了空值。