if (someSftphcFtp != null){
//登录sftp
sftp = new SFTPUtil(someSftphcFtp .getFtpusername(), someSftphcFtp .getFtppassword(), someSftphcFtp .getFtpip(), someSftphcFtp .getFtpport());
try {
sftp.login();
log.info("sftp->"+"用户名:"+someSftphcFtp .getFtpusername()+",密码是:"+someSftphcFtp .getFtppassword()+",ip是:"+someSftphcFtp .getFtpip()+",端口是:"+someSftphcFtp .getFtpport());
} catch (Exception e) {
log.error("登录sftp服务异常");
throw new Exception();
}
}
byte[] filebyte = new byte[0];
try {
//folderpath文件目录,zipFolderpath压缩文件名
filebyte = sftp.download(folderpath, zipFolderpath);
log.info("读取文件成功");
} catch (Exception e) {
log.error("下载压缩文件出错");
}
InputStream inputStream = new ByteArrayInputStream(filebyte);
//获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream), Charset.forName("GBK"));
//定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
ZipEntry ze = null;
while ((ze = zipInputStream.getNextEntry()) != null) {
System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");
//读取,java.util.zip.ZipInputStream.getNextEntry()方法读取下一个ZIP文件条目并将该流定位在条目数据的开头。
BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream, Charset.forName("UTF-8")));
String line;
StringBuffer stringBuffer = new StringBuffer();
//内容不为空,输出
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
// System.out.println(line);
}
String str = stringBuffer.toString();
System.out.println("当前压缩包文件中其中一个文件的内容:"+str);
//可以使用ze.getName()对文件名判断,来归类对应的文件
}
|