簡體   English   中英

使用java.util.zip轉換zip目錄功能以使用LZMA

[英]Transform zip directory function using java.util.zip to use 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();
            }
        }
    }

顯然是不可能的,您要做的是將所有文件打包到tar / zip中,然后應用lzma。 看看這個如何在Java中使用LZMA SDK進行壓縮/解壓縮

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM