4

(这是 X 发布到 Jsch 邮件列表 BTW)。我正在从数据库中读取数据并将其作为 byte[] 携带(用于跨中间件组件的传输)。

从那个字节 [] 我知道如何使用 GZIPOutputStream 类在本地文件系统上创建一个压缩文件。我想做的是使用 JSch SFTP 方法在远程文件系统上创建一个 gzip 压缩文件。

我已经压缩了数据的字节 [],并将其作为 InputStream 传递给 JSch 库,以便 SFTP 到远程文件目录(作为 .gz 文件)。但是,交付的文件具有意外的 EOF,无法“压缩”

gunzip:GlobalIssuer.xml.gz:文件意外结束

提醒我不是传输 .gz 文件内容的 byte[],它是数据库记录的内容

(相对)SSCCE如下:

byte[] content = "Content".getBytes();
// It does work (I promise!) returns a 'gzipped' byte[]
byte[] gzippedContent = gzipContent(content);
ByteArrayInputStream bais = new ByteArrayInputStream(gzippedContent);
channelSftp.put(bais, "Content.txt.gz");

gzipContent 方法:

private byte[] gzipContent(byte[] content)
{
    ByteArrayInputStream in = new ByteArrayInputStream(content);

    // Create stream to compress data and write it to the to file.
    GZIPOutputStream gzipOutputStream = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
        gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        byte[] buffer = new byte[4096];
        int bytes_read;
        while ((bytes_read = in.read(buffer)) != END_OF_FILE)
        {
            gzipOutputStream.write(buffer, 0, bytes_read);
        }

        // Return the gzipped content
        return byteArrayOutputStream.toByteArray();
    }
    catch (IOException e)
    {   
        // Altered from original to make this a SSCCE
        // Don't write exception handling like this at home!
        System.err.println("Unable to gzip content" + e.getMessage());
        return null;
    }
    /* 
     * Lots of closing streams with exception handling below.
     * I *think* I'm closing off streams in the right order
     * It's not triggering any of the System.err.println calls in any case
     * Of course System.err.println is bad, but this is a SSCCE
     */
    finally
    {
        try 
        {
            if (in != null)
            {
                in.close();
            }
        }
        catch (IOException e)
        {
            System.err.println("Was unable to close the Inputstream for gzipping, be aware of mem leak.");
        }
        try 
        {
            if (byteArrayOutputStream != null)
            {
                byteArrayOutputStream.close();
                if (gzipOutputStream != null)
                {
                    gzipOutputStream.close();
                }
            }
        }
        catch (IOException e)
        {
            System.err.println("Was unable to close the OutputStream(s) for gzipping, be aware of mem leak.");
        }
    }
}

以字节为单位的原始内容(“内容”):

0x750x6E0x630x6F0x6D0x700x720x650x730x730x650x640x430x6F0x6E0x740x650x6E0x74

压缩后的内容(“内容”)(以字节为单位):

0x1F0x8B0x080x000x000x000x000x000x000x00

或者:

1f8b 0800 0000 0000 0000 

使用 GZIPOutputStream 和 FileOutputStream 写出到本地文件系统的等效 gzipped 内容。

1f8b 0800 0000 0000 0000 2bcd 4bce cf2d  ..........+ÍKÎÏ-
284a 2d2e 4e4d 71ce cf2b 49cd 2b01 00f8  (J-.NMqÎÏ+IÍ+..ø
3987 5f13 0000 00                        9._....

我想我看到了问题。尽管内容已正确压缩,但我尚未创建压缩文件所需的校验和后缀(如果您在本地文件系统上执行此操作,则 GZIPOutputStream 会与 FileOutputStream 一起执行此操作)。所以基本上我错过了这个:

2bcd 4bce cf2d  ..........+ÍKÎÏ-
284a 2d2e 4e4d 71ce cf2b 49cd 2b01 00f8  (J-.NMqÎÏ+IÍ+..ø
3987 5f13 0000 00                        9._....

我在 Jsch 库中看不到可以解决问题的方法 - 这意味着我认为我错过了一些基本点。

4

1 回答 1

8

看起来您的问题在于将 GZipOutputStream 与 ByteArrayOutputStream 结合使用,并且与 JSch 完全无关。

GZipOutputStream 正在使用(通过其超类 DeflatorOutputStream)一个 Deflator 来完成实际工作。这个 deflator 被允许缓冲它认为合适的任何数量的数据,直到你使用它的finish()方法(通过流finish()close())说压缩文件已经完成。然后,这还将gzip包含校验和的页脚写入目标输出。

我认为您的问题可以通过在级联getByteArray之后移动或在其之前添加来解决。close()finish()

于 2011-06-04T12:07:50.397 回答