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

使用java.util.zip将zip目录函数转换为使用LZMA,java,zip,zipfile,lzma,Java,Zip,Zipfile,Lzma,我目前有一个函数makeBackup(),它将整个目录压缩成一个zip文件,但是文件太大了,所以我们决定切换到LZMA。 我们发现一个库可以这样做(),但是它似乎只压缩一个文件,而我们使用的zip函数允许将文件和目录添加到zip文件中 我们如何通过改变函数来实现LZMA的相同功能?我在下面添加了我们当前的功能: private static void makeBackup() { String backupPathString = "/home/backups";

我目前有一个函数makeBackup(),它将整个目录压缩成一个zip文件,但是文件太大了,所以我们决定切换到LZMA。 我们发现一个库可以这样做(),但是它似乎只压缩一个文件,而我们使用的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();
            }
        }
    }
private static void makeBackup()
{
字符串backupPathString=“/home/backups”;
/*zip远程文件*/
尝试
{
//要创建的zip文件的名称
字符串zipFilename=“backup.zip”;
//创建ZipOutputStream对象
zipOutStream zipOutStream=新zipOutStream(新文件输出流(zipFilename));
//要压缩的当前文件的路径
File zipFolder=新文件(backupPathString);
//获取路径前缀,以便zip文件不包含整个路径
//例如,如果要压缩的当前文件是/home/lalit/test
//打开的zip文件将包含test currentFile,而不是home/lalit/test currentFile
int len=zipFolder.getAbsolutePath().lastIndexOf(File.separator);
String baseName=zipFolder.getAbsolutePath().substring(0,len+1)+File.separator+“TodayBackup”;
zipFilesInPath(zipOutStream、backupPathString、baseName);
zipOutStream.flush();
zipOutStream.close();
}
捕获(IOE异常)
{
}
}
私有静态void zipFilesInPath(ZipOutStream ZipOutStream、字符串文件路径、字符串baseName)引发IOException
{
File currentFile=新文件(文件路径);
ArrayList fileArrayList=新的ArrayList(Arrays.asList(currentFile.listFiles());
if(fileArrayList.isEmpty())
{
字符串名称=currentFile.getAbsolutePath().substring(baseName.length());
ZipEntry ZipEntry=新ZipEntry(名称+“/”+”);
zipOutputStream.putNextEntry(Zippentry);
}
for(文件:fileArrayList)
{
if(file.isDirectory())
{
zipFilesInPath(ZipOutStream,file.getAbsolutePath(),baseName);
}
其他的
{
字符串名称=file.getAbsolutePath().substring(baseName.length());
ZipEntry ZipEntry=新ZipEntry(名称);
zipOutputStream.putNextEntry(Zippentry);
copy(新文件输入流(file),zipOutputStream);
zipOutputStream.closeEntry();
}
}
}
私有静态void unzipFilesToPath(ZipInputStream ZipInputStream,String fileExtractPath)引发IOException
{
ZipEntry入口;
while((entry=zipInputStream.getnextery())!=null)
{
整数计数;
字节[]数据=新字节[2048];
/*让我们制作所需的目录结构*/
File destFile=新文件(fileExtractPath,entry.getName());
File destinationParent=destFile.getParentFile();
//如果需要,创建父目录结构
destinationParent.mkdirs();
如果(!entry.isDirectory()&&!entry.getName().substring(entry.getName().length()-1).等于(“.”)
{
final FileOutputStream fos=新的FileOutputStream(fileExtractPath+File.separator+entry.getName());
最终缓冲输出流dest=新的缓冲输出流(fos,2048);
而((count=zipInputStream.read(data,02048))!=-1)
{
目的写入(数据,0,计数);
}
dest.flush();
dest.close();
}
}
}

显然不可能,您需要做的是将所有文件打包到tar/zip中,然后应用lzma。看看这个

如果你先用zip压缩,然后用lzma压缩,你有足够好的压缩率吗?没有。。如果我把复制了4次的同一个文件放在一个目录中,如果我直接用lzma压缩它,我得到的大小是先压缩然后lzma压缩的1/4