Java – Java 7zip压缩太大了

我有一个Java程序,它搜索具有昨天日期的文件夹并将其压缩为7zip文件并在最后删除它。现在我注意到我的程序生成的7zip存档文件太大了。当我使用像7-Zip文件管理器这样的程序来压缩我的文件时,它会生成一个大小为5 kb的存档,而我的程序会为相同的文件(大小为873 kb)生成737 kb的存档。现在我担心我的程序不会将其压缩为7zip文件,而是执行常用的zip文件。有没有办法在我的代码中更改某些内容,以便生成一个较小的7zip文件,如7-Zip文件管理器可以执行此操作?

package SevenZip;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;

public class SevenZipUtils {

    public static void main(String[] args) throws InterruptedException, IOException {

        String sourceFolder = "C:/Users/Ferid/Documents/Dates/";
        String outputZipFile = "/Users/Ferid/Documents/Dates";
        int sleepTime = 0;
        compress(sleepTime, outputZipFile, sourceFolder);
    }

    public static boolean deleteDirectory(File directory, int sleepTime) throws InterruptedException {
        if (directory.exists()) {
            File[] files = directory.listFiles();
            if (null != files) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        deleteDirectory(files[i], sleepTime);
                        System.out.println("Folder deleted: " + files[i]);
                    } else {
                        files[i].delete();
                        System.out.println("File deleted: " + files[i]);
                    }
                }
            }
        }
        TimeUnit.SECONDS.sleep(sleepTime);
        return (directory.delete());
    }

    public static void compress(int sleepTime, String outputZipFile, String sourceFolder)
            throws IOException, InterruptedException {

        // finds folder of yesterdays date
        final Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1); // date of yesterday
        String timeStamp = new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); // format the date
        System.out.println("Yesterday was " + timeStamp);

        if (sourceFolder.endsWith("/")) { // add yesterday folder to sourcefolder path
            sourceFolder = sourceFolder + timeStamp;
        } else {
            sourceFolder = sourceFolder + "/" + timeStamp;
        }

        if (outputZipFile.endsWith("/")) { // add yesterday folder name to outputZipFile path
            outputZipFile = outputZipFile + " " + timeStamp + ".7z";
        } else {
            outputZipFile = outputZipFile + "/" + timeStamp + ".7z";
        }

        File file = new File(sourceFolder);

        if (file.exists()) {
            try (SevenZOutputFile out = new SevenZOutputFile(new File(outputZipFile))) {
                addToArchiveCompression(out, file, ".");
                System.out.println("Files sucessfully compressed");

                deleteDirectory(new File(sourceFolder), sleepTime);
            }
        } else {
            System.out.println("Folder does not exist");
        }
    }

    private static void addToArchiveCompression(SevenZOutputFile out, File file, String dir) throws IOException {
        String name = dir + File.separator + file.getName();
        if (file.isFile()) {
            SevenZArchiveEntry entry = out.createArchiveEntry(file, name);
            out.putArchiveEntry(entry);

            FileInputStream in = new FileInputStream(file);
            byte[] b = new byte[1024];
            int count = 0;
            while ((count = in.read(b)) > 0) {
                out.write(b, 0, count);
            }
            out.closeArchiveEntry();
            in.close();
            System.out.println("File added: " + file.getName());
        } else if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children != null) {
                for (File child : children) {
                    addToArchiveCompression(out, child, name);
                }
            }
            System.out.println("Directory added: " + file.getName());
        } else {
            System.out.println(file.getName() + " is not supported");
        }
    }
}

我正在使用Apache Commons Compress

编辑:这是一个链接,我有一些Apache Commons Compress代码。


Commons Compress正在容器文件中为每个归档条目启动一个新块。请注意块计数器:

不是你希望的答案,但文档说它不支持“固体压缩” – 将几个文件写入一个块。请参阅此处文档中的第5段。

快速浏览一下其他一些支持LZMA压缩的Java库,但是我找不到一个可以在7-Zip的父容器文件格式中这样做的库。也许其他人知道另一种选择……

听起来像普通的zip文件格式(例如通过ZipOutputStream)不是一个选项?


使用7-Zip文件存档,它832 KB可以26.0 KB轻松压缩 文件:

  1. 获取它的JarSDK
  2. 选择LZMA Compression .java相关文件。
  3. Run向项目属性添加参数:e "D:\\2017ASP.pdf" "D:\\2017ASP.7z"e代表encode"input path" "output path"
  4. 运行项目[LzmaAlone.java]。

结果

Case1(.pdf文件):从。33,969 KB24,645 KB

Case2(.docx文件):从。832 KB26.0 KB

添加评论

友情链接:蝴蝶教程