使用java.util.zip转换zip目录函数以使用LZMA

时间:2013-05-30 08:53:50

标签: java zip zipfile lzma

我目前有一个函数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();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

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