如果你在使用 TarArchiveInputStream
时遇到 "Corrupted TAR archive" 的问题,可能是因为该方法读取的内容与预期的 TAR 文件格式不匹配。在这种情况下,可以尝试使用更灵活的方法来处理 TAR 文件。
下面是一种不使用 Apache Commons Compress 库的 Java 10+ 的解决方案,该方案使用 java.nio.file
包中的 FileSystems
和 Files
类:
import com.jcraft.jsch.*;
import java.io.*;
import java.nio.file.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.GZIPInputStream;
public class RemoteFileProcessor {
private static final String REMOTE_HOST = "your_remote_host";
private static final String REMOTE_USER = "your_remote_user";
private static final String REMOTE_PASSWORD = "your_remote_password";
private static final String REMOTE_FILE_PATH = "/path/to/your/23.tar.gz";
public static void main(String[] args) {
try {
// Connect to remote host
JSch jsch = new JSch();
Session session = jsch.getSession(REMOTE_USER, REMOTE_HOST, 22);
session.setPassword(REMOTE_PASSWORD);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
// Read remote file content
byte[] fileContent = readRemoteFile(session, REMOTE_FILE_PATH);
// Decompress the GZIP file
byte[] decompressedContent = decompressGzip(fileContent);
// Process the decompressed content (calculate MD5 and print file names)
processContent(decompressedContent);
// Disconnect from remote host
session.disconnect();
} catch (JSchException | IOException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static byte[] readRemoteFile(Session session, String remoteFilePath) throws IOException, JSchException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp")) {
sftpChannel.connect();
sftpChannel.get(remoteFilePath, baos);
return baos.toByteArray();
}
}
private static byte[] decompressGzip(byte[] data) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzipIn = new GZIPInputStream(bis)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = gzipIn.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
}
private static void processContent(byte[] content) throws IOException, NoSuchAlgorithmException {
Path tempFile = Files.createTempFile("temp", ".tar");
try {
Files.write(tempFile, content);
try (FileSystem fs = FileSystems.newFileSystem(tempFile, null)) {
Files.walk(fs.getPath("/"))
.filter(path -> !Files.isDirectory(path))
.forEach(path -> {
try {
byte[] fileBytes = Files.readAllBytes(path);
String md5 = calculateMD5(fileBytes);
System.out.println("File Name: " + path.getFileName());
System.out.println("MD5: " + md5);
} catch (IOException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
});
}
} finally {
Files.delete(tempFile);
}
}
private static String calculateMD5(byte[] data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5 = md.digest(data);
StringBuilder result = new StringBuilder();
for (byte b : md5) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
这段代码会将 TAR 文件写入临时文件,然后使用 java.nio.file
包来处理。如果你仍然遇到问题,可能需要进一步检查 TAR 文件的格式。