How to estimate zip file size in java before creating it

后端 未结 7 424
时光取名叫无心
时光取名叫无心 2021-01-08 00:27

I am having a requirement wherein i have to create a zip file from a list of available files. The files are of different types like txt,pdf,xml etc.I am using java util clas

7条回答
  •  花落未央
    2021-01-08 01:21

    just wanted to share how we implemented manual way

                int maxSizeForAllFiles = 70000; // Read from property
            int sizePerFile = 22000; // Red from property
            /**
             * Iterate all attachment list to verify if ZIP is required
             */
            for (String attachFile : inputAttachmentList) {
                File file = new File(attachFile);
                totalFileSize += file.length();
                /**
                 * if ZIP required ??? based on the size
                 */
                if (file.length() >= sizePerFile) {
                    toBeZipped = true;
                    logger.info("File: "
                                + attachFile
                                    + " Size: "
                                    + file.length()
                                    + " File required to be zipped, MAX allowed per file: "
                                    + sizePerFile);
                    break;
                }
            }
            /**
             * Check if all attachments put together cross MAX_SIZE_FOR_ALL_FILES
             */
            if (totalFileSize >= maxSizeForAllFiles) {
                toBeZipped = true;
            }
            if (toBeZipped) {
                // Zip Here iterating all attachments
            }
    

提交回复
热议问题