用上面的内容还是获取到34.tar.gz压缩包里面每个文件的MD5值 entry = tarInput.getNextTarEntry()这行提示 Corrupted TAR archive.

本文提供了一种在Java10及以上版本中,不依赖ApacheCommonsCompress库解决TAR文件读取问题的解决方案。通过连接远程主机、读取并解压缩GZIP文件,然后使用`java.nio.file`处理TAR内容,包括计算MD5校验和打印文件名。
摘要由CSDN通过智能技术生成

如果你在使用 TarArchiveInputStream 时遇到 "Corrupted TAR archive" 的问题,可能是因为该方法读取的内容与预期的 TAR 文件格式不匹配。在这种情况下,可以尝试使用更灵活的方法来处理 TAR 文件。

下面是一种不使用 Apache Commons Compress 库的 Java 10+ 的解决方案,该方案使用 java.nio.file 包中的 FileSystemsFiles 类:

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 文件的格式。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值