压缩前如何获取压缩文件的确切大小? [英] How to get exact size of zipped file before zipping?

查看:201
本文介绍了压缩前如何获取压缩文件的确切大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下独立类在压缩之前计算压缩文件的大小.我正在使用 0 级压缩,但仍然有几个字节的差异.你能帮我弄一下确切的尺寸吗?

I am using following standalone class to calculate size of zipped files before zipping. I am using 0 level compression, but still i am getting a difference of few bytes. Can you please help me out in this to get exact size?

快速帮助将不胜感激.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FilenameUtils;


public class zipcode {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub



         try {
             CRC32 crc = new CRC32();

                byte[] b = new byte[1024]; 
                File file = new File("/Users/Lab/Desktop/ABC.xlsx");
            FileInputStream in = new FileInputStream(file);
            crc.reset();
                // out put file 
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/Users/Lab/Desktop/ABC.zip"));


                // name the file inside the zip  file 

                ZipEntry entry = new ZipEntry("ABC.xlsx");
                entry.setMethod(ZipEntry.DEFLATED);
                entry.setCompressedSize(file.length());
                entry.setSize(file.length());
                entry.setCrc(crc.getValue());
                out.setMethod(ZipOutputStream.DEFLATED);
                out.setLevel(0);
                //entry.setCompressedSize(in.available());
                //entry.setSize(in.available());
                //entry.setCrc(crc.getValue());


                out.putNextEntry(entry); 
                // buffer size

                int count;

                while ((count = in.read(b)) > 0) {
                    System.out.println();
                    out.write(b, 0, count);
                }
                out.close();
                in.close();         
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

推荐答案

首先,我不相信您为什么需要这样做的解释.如果在开始上传之前需要知道文件大小,则您的系统设计或实现有问题.

Firstly, I'm not convinced by explanation for why you need to do this. There is something wrong with your system design or implementation if it is necessary to know the file size before you start uploading.

话虽如此,解决方案基本上是在服务器端创建 ZIP 文件,以便您在开始将其上传到客户端之前知道其大小:

Having said that, the solution is basically to create the ZIP file on the server side so that you know its size before you start uploading it to the client:

  • 将 ZIP 文件写入临时文件并从中上传.

  • Write the ZIP file to a temporary file and upload from that.

将 ZIP 文件写入内存中的缓冲区并从中上传.

Write the ZIP file to an buffer in memory and upload from that.

如果服务器端既没有文件空间也没有内存空间,那么:

If you don't have either the file space or the memory space on the server side, then:

  • 创建接收器"输出流,它只计算写入的字节数以计算标称文件大小.

  • Create "sink" outputStream that simply counts the bytes that are written to calculate the nominal file size.

创建/写入 ZIP 文件到接收器,并捕获文件大小.

Create / write the ZIP file to the sink, and capture the file size.

打开连接进行上传.

发送包含文件大小的元数据.

Send the metadata including the file size.

再次创建/写入 ZIP,写入套接字流...或其他.

Create / write the ZIP a second time, writing to the socket stream ... or whatever.

如果有帮助的话,这 3 种方法都可以让您创建和发送压缩的 ZIP.

These 3 approaches will all allow you to create and send a compressed ZIP, if that is going to help.

如果您坚持尝试一次性完成此操作,那么您将需要详细阅读 ZIP 文件规范……并进行一些杂乱的算术.帮助您可能超出了 SO 问题的范围.

If you insist on trying to do this on-the-fly in one pass, then you are going to need to read the ZIP file spec in forensic detail ... and do some messy arithmetic. Helping you is probably beyond the scope of a SO question.

这篇关于压缩前如何获取压缩文件的确切大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆