简体   繁体   English

如何在 Java 中从另一个文件创建 GZIP 文件而不创建新的物理文件

[英]How to create a GZIP file in Java from another file without creating a new physical file

I was working with this example:我正在处理这个例子:

public class GZipExample {

    public static void main(String[] args) {

        // compress a file
        Path source = Paths.get("/home/test/sitemap.xml");
        Path target = Paths.get("/home/test/sitemap.xml.gz");

        if (Files.notExists(source)) {
            System.err.printf("The path %s doesn't exist!", source);
            return;
        }

        try {

            GZipExample.compressGzip(source, target);

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

    }

    // copy file (FileInputStream) to GZIPOutputStream
    public static void compressGzip(Path source, Path target) throws IOException {

        try (GZIPOutputStream gos = new GZIPOutputStream(
                                      new FileOutputStream(target.toFile()));
             FileInputStream fis = new FileInputStream(source.toFile())) {

            // copy file
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                gos.write(buffer, 0, len);
            }

        }

    }

I would like only to create the gzip file in memory and not physically on disk.我只想在内存中创建 gzip 文件,而不是在磁盘上物理创建。 I'm not sure if that is possible using some kind of stream or in-memory file?我不确定是否可以使用某种流或内存文件?

You can write to any OutputStream you want, meaning that it does not need to be a FileOutputStream .您可以写入任何所需的OutputStream ,这意味着它不需要是FileOutputStream You can use a ByteArrayOutputStream instead:您可以改用ByteArrayOutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (GZIPOutputStream gos = new GZIPOutputStream(baos);
     FileInputStream fis = new FileInputStream(source.toFile())) {

        // copy file
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) > 0) {
            gos.write(buffer, 0, len);
        }
}
byte[] data = baos.toByteArray();

The gzipped data is now a byte[] in your memory.压缩后的数据现在是您记忆中的一个byte[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在FTP服务器上创建XML文件而不创建物理文件本地 - Create XML file on FTP server without creating physical file localy java - 创建空GZIP文件 - java - creating empty GZIP file 拆分xml而不在Java中创建新元素到另一个文件 - Splitting an xml without creating new element in java to another file 如何直接从内容创建 java.io.File 实例而不在磁盘上创建临时文件? - How to create a java.io.File instance diectly from content without creating a temporary file on disk ? 如何在不创建新文件的情况下更新文件 - how to update the file without creating new file 如何发送zip文件而不在实际位置上创建它? - How to send zip file without creating it on physical location? 为什么 StandardOpenOption.CREATE_NEW 没有创建要在 Java 中读取的文件? - Why is StandardOpenOption.CREATE_NEW not creating a file to be read from in Java? 如何在不使用第三方库的情况下使用Java中另一个CSV文件中的值创建CSV文件 - How to create a CSV file using the values from another CSV file in Java without using third party library 从字节数组创建文件对象时指定文件名(不创建物理文件) - Specifying the filename while creating a File Object it from byte array (without creating a physical file) 动态编译而不创建物理文件 - Dynamic Compiling Without Create Physical File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM