用java提取.rar文件

编程入门 行业动态 更新时间:2024-10-27 14:31:04
本文介绍了用java提取.rar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种方法来解压缩 .rar 使用Java的文件以及我在哪里搜索我最后使用相同的工具 - JavaUnRar 。我一直在寻找解压缩 .rar 这样的文件,但我似乎发现这样做的所有方式都非常长而且很尴尬,就像在这个例子

I am looking a ways to unzip .rar files using Java and where ever I search i keep ending up with the same tool - JavaUnRar. I have been looking into unzipping .rar files with this but all the ways i seem to find to do this are very long and awkward like in this example

我目前能够提取 .tar , .tar.gz , .zip 和 .jar 20行代码或更少的文件,因此必须有一种更简单的方法来提取 .rar 文件,有人知道吗?

I am currently able to extract .tar, .tar.gz, .zip and .jar files in 20 lines of code or less so there must be a simpler way to extract .rar files, does anybody know?

如果它对任何人有帮助这就是我用来代码提取 .zip 和 .jar 文件,它适用于两者

Just if it helps anybody this is the code that I am using to extract both .zip and .jar files, it works for both

public void getZipFiles(String zipFile, String destFolder) throws IOException { BufferedOutputStream dest = null; ZipInputStream zis = new ZipInputStream( new BufferedInputStream( new FileInputStream(zipFile))); ZipEntry entry; while (( entry = zis.getNextEntry() ) != null) { System.out.println( "Extracting: " + entry.getName() ); int count; byte data[] = new byte[BUFFER]; if (entry.isDirectory()) { new File( destFolder + "/" + entry.getName() ).mkdirs(); continue; } else { int di = entry.getName().lastIndexOf( '/' ); if (di != -1) { new File( destFolder + "/" + entry.getName() .substring( 0, di ) ).mkdirs(); } } FileOutputStream fos = new FileOutputStream( destFolder + "/" + entry.getName() ); dest = new BufferedOutputStream( fos ); while (( count = zis.read( data ) ) != -1) dest.write( data, 0, count ); dest.flush(); dest.close(); } }

推荐答案

你能够提取 .gz , .zip , .jar 文件,因为它们使用Java SDK中内置的压缩​​算法的数量。

You are able to extract .gz, .zip, .jar files as they use number of compression algorithms built into the Java SDK.

RAR 格式的情况略有不同。 RAR是专有归档文件格式。 RAR许可不允许将其包含在Java SDK等软件开发工具中。

The case with RAR format is a bit different. RAR is a proprietary archive file format. RAR license does not allow to include it into software development tools like Java SDK.

unrar 文件的最佳方法是使用第三方库,例如 junrar 。

The best way to unrar your files will be using 3rd party libraries such as junrar.

您可以在SO问题中找到对其他Java RAR库的一些引用使用java进行ROM存档。另外问题如何使用java程序将文本文件压缩为rar格式解释了不同的解决方法(例如使用运行时)。

You can find some references to other Java RAR libraries in SO question RAR archives with java. Also SO question How to compress text file to rar format using java program explains more on different workarounds (e.g. using Runtime).

更多推荐

用java提取.rar文件

本文发布于:2023-11-06 13:16:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   java   rar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!