使用 GZIP 将 Java 文件压缩转换为 Python3

发布于 2025-01-18 12:24:06 字数 1708 浏览 3 评论 0原文

我需要将文件压缩为我们国家税收法规实体要求的特定格式,并且必须在基本64中进行编码。

我在Python3上工作,并尝试使用以下代码进行压缩:

import gzip

# Work file generated before and stored in BytesBuffer
my_file = bytes_buffer.getvalue()

def compress(work_file):
   encoded_work_file = base64.b64encode(work_file)
   compressed_work_file = gzip.compress(encoded_work_file )
   return base64.b64encode(compressed_work_file )
   
compress(my_file)

现在,税收实体返回了有关未知压缩格式的错误消息。 幸运的是,他们为我们提供了以下Java示例代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class DemoGZIP {

    private final byte[] BUFFER = new byte[1024];

    /**
     * @param work_file File to compress
     *      The file is compressed over the original file name with the extension .zip
     * @return boolean 
     *      TRUE success
     *      FALSE failure
     */
    public boolean compress(File work_file ) {                
        try (GZIPOutputStream  out = new GZIPOutputStream (new FileOutputStream(work_file .getAbsolutePath() + ".zip"));
                FileInputStream in = new FileInputStream(work_file )) {
            int len;
            while ((len = in.read(BUFFER)) != -1) {
                out.write(BUFFER, 0, len);
            }
            out.close();
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            return false;
        }
        return true;
    }

问题是我没有任何在Java上工作的经验,并且不了解提供的代码很多。

有人可以帮助我调整我的代码来完成提供的代码在Python中所做的事情吗?

I need to compress a file into a specific format that is required by our country's tax regulation entity and it has to be sent encoded in base64.

I work on Python3 and attempted to do the compression with the following code:

import gzip

# Work file generated before and stored in BytesBuffer
my_file = bytes_buffer.getvalue()

def compress(work_file):
   encoded_work_file = base64.b64encode(work_file)
   compressed_work_file = gzip.compress(encoded_work_file )
   return base64.b64encode(compressed_work_file )
   
compress(my_file)

Now the tax entity returns an error message about an unknown compression format.
Luckily, they provided us the following Java example code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class DemoGZIP {

    private final byte[] BUFFER = new byte[1024];

    /**
     * @param work_file File to compress
     *      The file is compressed over the original file name with the extension .zip
     * @return boolean 
     *      TRUE success
     *      FALSE failure
     */
    public boolean compress(File work_file ) {                
        try (GZIPOutputStream  out = new GZIPOutputStream (new FileOutputStream(work_file .getAbsolutePath() + ".zip"));
                FileInputStream in = new FileInputStream(work_file )) {
            int len;
            while ((len = in.read(BUFFER)) != -1) {
                out.write(BUFFER, 0, len);
            }
            out.close();
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            return false;
        }
        return true;
    }

The problem is that I do not have any experience working on Java and do not understand much of the provided code.

Can someone please help me adapt my code to do what the provided code does in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2025-01-25 12:24:06

如注释中指出的那样,Java代码不会进行base64编码,并错误地命名了文件。它绝对不是zip文件,而是一个GZIP文件。后缀应为“ .gz”。尽管我怀疑这个名字对您的税务机构很重要。

更重要的是,您是用Base64 两次编码。从您的描述来看,GZIP压缩后只能这样做一次。从Java代码中,您不应该完全编码Base64!您需要对此进行澄清。

As noted in the comment, the Java code does not do Base64 coding, and names the resulting file incorrectly. It is most definitely not a zip file, it is a gzip file. The suffix should be ".gz". Though I doubt that the name matters to your tax agency.

More importantly, you are encoding with Base64 twice. From your description, you should only do that once, after gzip compression. From the Java code, you shouldn't do Base64 encoding at all! You need to get clarification on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文