0

我目前有一个函数 makeBackup() 可以将整个目录压缩成一个 zip 文件,但是文件太大了,所以我们决定切换到 LZMA。我们找到了一个执行此操作的库(lzma-java),但它似乎只压缩单个文件,而我们使用的 zip 函数允许将文件和目录添加到 zip 文件中。

我们如何通过改变我们的函数来用 LZMA 实现相同的功能?我在下面添加了我们当前的功能:

private static void makeBackup()
    {
        String backupPathString = "/home/backups";
        /* zip remote file */
        try
        {
            //name of zip file to create
            String zipFilename = "backup.zip";

            //create ZipOutputStream object
            ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFilename));

            //path to the currentFile to be zipped
            File zipFolder = new File(backupPathString);

            //get path prefix so that the zip file does not contain the whole path
            // eg. if currentFile to be zipped is /home/lalit/test
            // the zip file when opened will have test currentFile and not home/lalit/test currentFile
            int len = zipFolder.getAbsolutePath().lastIndexOf(File.separator);
            String baseName = zipFolder.getAbsolutePath().substring(0, len + 1) + File.separator + "todaybackups";

            zipFilesInPath(zipOutStream, backupPathString, baseName);
            zipOutStream.flush();
            zipOutStream.close();
        }
        catch (IOException e)
        {
        }
    }

    private static void zipFilesInPath(ZipOutputStream zipOutputStream, String filePath, String baseName) throws IOException
    {
        File currentFile = new File(filePath);
        ArrayList<File> filesArrayList = new ArrayList<File>(Arrays.asList(currentFile.listFiles()));
        if (filesArrayList.isEmpty())
        {
            String name = currentFile.getAbsolutePath().substring(baseName.length());
            ZipEntry zipEntry = new ZipEntry(name + "/" + ".");
            zipOutputStream.putNextEntry(zipEntry);
        }
        for (File file : filesArrayList)
        {
            if (file.isDirectory())
            {
                zipFilesInPath(zipOutputStream, file.getAbsolutePath(), baseName);
            }
            else
            {
                String name = file.getAbsolutePath().substring(baseName.length());
                ZipEntry zipEntry = new ZipEntry(name);
                zipOutputStream.putNextEntry(zipEntry);
                IOUtils.copy(new FileInputStream(file), zipOutputStream);
                zipOutputStream.closeEntry();
            }
        }
    }

    private static void unzipFilesToPath(ZipInputStream zipInputStream, String fileExtractPath) throws IOException
    {
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null)
        {
            int count;
            byte[] data = new byte[2048];

            /*let's make the directory structure needed*/
            File destFile = new File(fileExtractPath, entry.getName());
            File destinationParent = destFile.getParentFile();
            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory() && !entry.getName().substring(entry.getName().length() - 1).equals("."))
            {
                final FileOutputStream fos = new FileOutputStream(fileExtractPath + File.separator + entry.getName());
                final BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((count = zipInputStream.read(data, 0, 2048)) != -1)
                {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
        }
    }
4

1 回答 1

0

显然是不可能的,您要做的就是将所有文件打包成 tar/zip,然后应用 lzma。看看这个How to use LZMA SDK to compress/decompress in Java

于 2013-05-30T12:56:59.030 回答