简体   繁体   English

使用java.util.zip转换zip目录功能以使用LZMA

[英]Transform zip directory function using java.util.zip to use LZMA

I currently have a function makeBackup() which zips an entire directory into a zip file, the files are however too big so we decided to switch to LZMA. 我目前有一个函数makeBackup(),它将整个目录压缩为一个zip文件,但是文件太大,因此我们决定切换到LZMA。 We found a library which does this ( lzma-java ) however it seems to compress only a single file, while the zip function we used permits to add files and directories to a zip file. 我们找到了一个执行此操作的库( lzma-java ),但是它似乎仅压缩单个文件,而我们使用的zip函数允许将文件和目录添加到zip文件中。

How can we implement the same with LZMA by changing our function? 如何通过更改功能用LZMA实现相同的功能? I added our current function below: 我在下面添加了当前功能:

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();
            }
        }
    }

apparently is not possible, and what you have to do is package all your files into a tar/zip and then apply the lzma. 显然是不可能的,您要做的是将所有文件打包到tar / zip中,然后应用lzma。 take a look at this How to use LZMA SDK to compress/decompress in Java 看看这个如何在Java中使用LZMA SDK进行压缩/解压缩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM