gpt4 book ai didi

java - 如何使用 Java 压缩 SFTP 或 FTP 服务器中的文件?

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:01 26 4
gpt4 key购买 nike

我可以在本地计算机上压缩文件,但我想通过 Java 在 SFTP 服务器上压缩 (.zip) 文件。如何传递 URL 以及如何压缩 SFTP 或 FTP 服务器中的文件?

下面是我的本地系统,它与我需要在 SFTP 中实现的功能相同。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressToZip {

public static void main(String[] args) throws Exception{
String pattern = "ddMMyyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
String sourceFolderName = "C:\\Users\\Desktop\\BackUpFiles\\"+date;

File folder = new File("C:\\Users\\Desktop\\BackUpFileZip");
String outputFileName = folder+"\\"+date+".zip";
if(!folder.exists()){
folder.mkdir();
}else{
System.out.println("Folder Exist.....");
}


System.currentTimeMillis();
FileOutputStream fos = new FileOutputStream(outputFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
//level - the compression level (0-9)
zos.setLevel(9);

System.out.println("Begin to compress folder : " + sourceFolderName + " to " + outputFileName);
addFolder(zos, sourceFolderName, sourceFolderName);

zos.close();
System.out.println("Program ended successfully!");
}

private static void addFolder(ZipOutputStream zos,String folderName,String baseFolderName)throws Exception{
File f = new File(folderName);
if(f.exists()){

if(f.isDirectory()){
//Thank to peter
//For pointing out missing entry for empty folder
if(!folderName.equalsIgnoreCase(baseFolderName)){
String entryName = folderName.substring(baseFolderName.length()+1,folderName.length()) + File.separatorChar;
System.out.println("Adding folder entry " + entryName);
ZipEntry ze= new ZipEntry(entryName);
zos.putNextEntry(ze);
}
File f2[] = f.listFiles();
for(int i=0;i<f2.length;i++){
addFolder(zos,f2[i].getAbsolutePath(),baseFolderName);
}
}else{
//add file
//extract the relative name for entry purpose
String entryName = folderName.substring(baseFolderName.length()+1,folderName.length());
System.out.print("Adding file entry " + entryName + "...");
ZipEntry ze= new ZipEntry(entryName);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(folderName);
int len;
byte buffer[] = new byte[1024];
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
System.out.println("OK!");

}
}else{
System.out.println("File or directory not found " + folderName);
}

}

}

如何给出SFTP或FTP服务器地址的sourceFolderNameoutputFileName

为了连接到 SFTP,我使用以下代码:

public class JschTestDownload {

public static void main(String s[]) {

JSch jsch = new JSch();

Session session = null;
// Remote machine host name or IP
String hostName = "xxx.xxx.xx.xxx";
// User name to connect the remote machine
String userName = "xxxxx";
// Password for remote machine authentication
String password = "xxxx";
// Source file path on local machine
String srcFilePath = "/Inbound/testSFTP.txt";
// Destination directory location on remote machine

String destinationLocation = "C:/Users/Desktop/SftpUpDown/TestDownload";
try {
// Getting the session
session = jsch.getSession(userName, hostName,22);

// Ignore HostKeyChecking
session.setConfig("StrictHostKeyChecking", "no");

// set the password for authentication
session.setPassword(password);
System.out.println("try to get the connection");
session.connect();
System.out.println("got the connection");
// Getting the channel using sftp
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;

sftpChannel.cd("/Inbound/");


// Exist the channel
sftpChannel.exit();

// Disconnect the session
session.disconnect();

} catch (JSchException e) {
e.printStackTrace();

} catch (SftpException e) {
e.printStackTrace();
}
}
}

通过使用上面的代码,我能够获取文件和放置文件。但我想通过使用第一个代码来压缩 SFTP 服务器中的文件。我需要在我的计算机中运行该代码并在服务器上压缩文件并下载到我的计算机。我该怎么做?给我一些指导。

最佳答案

你不能。无法通过 FTP 或 SFTP 协议(protocol)就地压缩文件。

您所能做的就是下载文件,在本地压缩并上传回来。您似乎可以做什么。

这并不意味着您必须将文件存储在本地(临时)。您可以使用流在内存中完成所有这些操作。


当然,如果您可以通过 shell 访问服务器,则可以使用 SSH 协议(protocol)连接并运行 zip(或类似的)命令来压缩文件。但这不是 SFTP/FTP。


相关问题:

关于java - 如何使用 Java 压缩 SFTP 或 FTP 服务器中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42626196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com