网站首页 > 文章中心 > 其它

java读取rar代码

作者:小编 更新时间:2023-10-04 12:29:07 浏览量:210人看过

如何在java中解压zip和rar文件

java中有zip包,可以使用

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

rar的只能用第三方api,比如junrar

java读取压缩文件并压缩

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;

public class JZip {

public static boolean bOverWrite;//是否覆盖同名文件 取值范围为True和False

@SuppressWarnings("unchecked")

private static ArrayList AllFiles = new ArrayList();

public static String sErrorMessage;

private String zipFilePath;

public ListFile srcMap;

public JZip () {

// bOverWrite=true;

public JZip(String zipFilePath) throws FileNotFoundException, IOException {

this.zipFilePath = zipFilePath;

public static ArrayList extract (String sZipPathFile , String sDestPath) {

ArrayList AllFileName = new ArrayList();

try {

//先指定压缩档的位置和档名,建立FileInputStream对象

FileInputStream fins = new FileInputStream(sZipPathFile);

//将fins传入ZipInputStream中

ZipInputStream zins = new ZipInputStream(fins);

ZipEntry ze = null;

while ((ze = zins.getNextEntry()) != null) {

File zfile = new File(sDestPath ◆ ze.getName());

File fpath = new File(zfile.getParentFile().getPath());

if (ze.isDirectory()) {

if (!zfile.exists())

zfile.mkdirs();

zins.closeEntry();

if (!fpath.exists())

fpath.mkdirs();

FileOutputStream fouts = new FileOutputStream(zfile);

int i;

AllFileName.add(zfile.getAbsolutePath());

while ((i = zins.read(ch)) != -1)

fouts.write(ch, 0, i);

fouts.close();

fins.close();

zins.close();

sErrorMessage = "OK";

} catch (Exception e) {

System.err.println("Extract error:" ◆ e.getMessage());

sErrorMessage = e.getMessage();

AllFiles.clear();

return AllFileName;

@SuppressWarnings({ "unchecked", "static-access" })

public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {

String sPath;

//先指定压缩档的位置及档名,建立一个FileOutputStream

FileOutputStream fos = new FileOutputStream(sZipPathFile);

//建立ZipOutputStream并将fos传入

ZipOutputStream zos = new ZipOutputStream(fos);

//设置压缩比

zos.setLevel(iCompressLevel);

if (bIsPath == true) {

searchFiles(sPathFile);

sPath = sPathFile;

File myfile = new File(sPathFile);

sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) ◆ 1);

AllFiles.add(myfile);

Object[] myobject = AllFiles.toArray();

//每个档案要压缩,都要透过ZipEntry来处理

FileInputStream fis = null;

BufferedReader in = null;

for (int i = 0 ; i myobject.length ; i◆◆) {

File myfile = (File) myobject[i];

if (myfile.isFile()) {

//以档案的名字当Entry,也可以自己再加上额外的路径

//例如 ze=new ZipEntry("test\\"◆myfiles[i].getName());

//如此压缩档内的每个档案都会加test这个路径

ze = new ZipEntry(myfile.getPath().substring((sPath).length()));

//将ZipEntry透过ZipOutputStream的putNextEntry的方式送进去处理

fis = new FileInputStream(myfile);

zos.putNextEntry(ze);

int len = 0;

//开始将原始档案读进ZipOutputStream

while ((len = in.read()) != -1) {

zos.write(len);

fis.close();

zos.closeEntry();

zos.close();

fos.close();

System.err.println("Compress error:" ◆ e.getMessage());

/*

这是一个递归过程,功能是检索出所有的文件名称

dirstr:目录名称

*/

private static void searchFiles (String dirstr) {

File tempdir = new File(dirstr);

if (tempdir.exists()) {

if (tempdir.isDirectory()) {

File[] tempfiles = tempdir.listFiles();

for (int i = 0 ; i tempfiles.length ; i◆◆) {

if (tempfiles[i].isDirectory())

searchFiles(tempfiles[i].getPath());

else {

AllFiles.add(tempfiles[i]);

AllFiles.add(tempdir);

public String getZipFilePath() {

return zipFilePath;

public void setZipFilePath(String zipFilePath) {

/**

* 解析zip文件得到文件名

* @return

* @throws FileNotFoundException

* @throws IOException

public boolean parserZip() throws FileNotFoundException, IOException {

FileInputStream fis = new FileInputStream(zipFilePath);

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

srcMap = new ArrayListFile();

while ((entry = zis.getNextEntry()) != null) {

File file = new File(zipFilePath ◆ File.separator ◆ entry.getName());

srcMap.add(file);

zis.close();

return true;

} catch (IOException e) {

return false;

*

* @param zipFileName 待解压缩的ZIP文件

* @param extPlace 解压后的文件夹

public static void extZipFileList(String zipFileName, String extPlace) {

ZipInputStream in = new ZipInputStream(new FileInputStream(

zipFileName));

File files = new File(extPlace);

files.mkdirs();

ZipEntry entry = null;

while ((entry = in.getNextEntry()) != null) {

String entryName = entry.getName();

File file = new File(files ◆ entryName);

file.mkdirs();

System.out.println("创建文件夹:" ◆ entryName);

OutputStream os = new FileOutputStream(files◆File.separator ◆ entryName);

// Transfer bytes from the ZIP file to the output file

int len;

while ((len = in.read(buf)) 0) {

os.write(buf, 0, len);

os.close();

in.closeEntry();

System.out.println("解压文件:" ◆ entryName);

@SuppressWarnings("static-access")

public static void main(String args[]){

java 调用rar命令 对rar文件进行加减密,提供思路即可!!

import java.io.BufferedOutputStream;

import java.util.Enumeration;

import java.util.zip.ZipFile;

public class Test {

Enumeration enu = null;// 创建一个枚举型引用

ZipFile zf = null; // 创建一个Zip文件引用

ZipEntry ze = null;// 创建一个进行入Zip文件的引用

BufferedInputStream bis = null; // 创建一个字节型输入流

BufferedOutputStream bos = null;// 创建一个字节型输出流

public void zipFiles(){

File f = new File("d:\\line.zip");

zf = new ZipFile(f);

enu = zf.entries();

while (enu.hasMoreElements()) {

ze = (ZipEntry) enu.nextElement();

new File("d:\\" ◆ ze.getName()).mkdirs();

bis = new BufferedInputStream(zf.getInputStream(ze));

FileOutputStream fos = new FileOutputStream("d:\\"

◆ ze.getName());

bos = new BufferedOutputStream(fos, bu);

while ((count = bis.read()) != -1) {

bos.write(buffer, 0, count);

bos.flush();

bos.close();

bis.close();

e.printStackTrace();

public static void main(String[] args) {

new Test().zipFiles();

java中怎么解压rar文件 到指定文件目录中

①代码如下:

[java] view plain copy

* 将文件夹下面的文件

* 打包成zip压缩文件

* @author admin

public final class FileToZip {

private FileToZip(){}

* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下

* @param sourceFilePath :待压缩的文件路径

* @param zipFilePath :压缩后存放路径

* @param fileName :压缩后文件的名称

public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){

boolean flag = false;

File sourceFile = new File(sourceFilePath);

BufferedInputStream bis = null;

FileOutputStream fos = null;

ZipOutputStream zos = null;

if(sourceFile.exists() == false){

System.out.println("待压缩的文件目录:"◆sourceFilePath◆"不存在.");

}else{

File zipFile = new File(zipFilePath ◆ "/" ◆ fileName ◆".zip");

if(zipFile.exists()){

System.out.println(zipFilePath ◆ "目录下存在名字为:" ◆ fileName ◆".zip" ◆"打包文件.");

File[] sourceFiles = sourceFile.listFiles();

if(null == sourceFiles || sourceFiles.length1){

System.out.println("待压缩的文件目录:" ◆ sourceFilePath ◆ "里面不存在文件,无需压缩.");

fos = new FileOutputStream(zipFile);

zos = new ZipOutputStream(new BufferedOutputStream(fos));

for(int i=0;isourceFiles.length;i◆◆){

//创建ZIP实体,并添加进压缩包

ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());

zos.putNextEntry(zipEntry);

//读取待压缩的文件并写进压缩包里

fis = new FileInputStream(sourceFiles[i]);

int read = 0;

zos.write(bufs,0,read);

flag = true;

} catch (FileNotFoundException e) {

throw new RuntimeException(e);

} finally{

//关闭流

if(null != bis) bis.close();

if(null != zos) zos.close();

return flag;

public static void main(String[] args){

String sourceFilePath = "D:\\TestFile";

String zipFilePath = "D:\\tmp";

boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);

if(flag){

System.out.println("文件打包成功!");

System.out.println("文件打包失败!");

/span

文件打包成功!

java怎么读取Zip和RAR里面的文件啊?

java.util.zip.*这个包下有ZipInputStream和ZipOutputStream这两个类,用于ZIP的读入解压,和生成ZIP文件的打包.

import java.io.InputStream;

import java.util.zip.GZIPInputStream;

import java.io.DataInputStream;

* Description: 此类用于...

* @version 1.0.0

public class ZipManager {

* zip压缩功能测试. 将d:\\temp\\zipout目录下的所有文件连同子目录压缩到d:\\temp\\out.zip.

* @param baseDir 所要压缩的目录名(包含绝对路径)

* @param objFileName 压缩后的文件名

* @throws Exception

public void createZip(String baseDir, String objFileName) throws Exception {

File folderObject = new File(baseDir);

if (folderObject.exists()){

List fileList = getSubFiles(new File(baseDir));

//压缩文件名

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFileName));

int readLen = 0;

for (int i = 0; i fileList.size(); i◆◆) {

File f = (File) fileList.get(i);

System.out.println("Adding: " ◆ f.getPath() ◆ f.getName());

//创建一个ZipEntry,并设置Name和其它的一些属性

ze = new ZipEntry(getAbsFileName(baseDir, f));

ze.setSize(f.length());

ze.setTime(f.lastModified());

//将ZipEntry加到zos中,再写入实际的文件内容

InputStream is = new BufferedInputStream(new FileInputStream(f));

zos.write(buf, 0, readLen);

is.close();

System.out.println("done...");

throw new Exception("this folder isnot exist!");

* zip压缩功能测试. 将指定文件压缩后存到一压缩文件中

* @param baseDir 所要压缩的文件名

* @return 压缩后文件的大小

public long createFileToZip(String zipFilename,String sourceFileName) throws Exception {

File sourceFile = new File(sourceFileName);

File objFile = new File(zipFilename);

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

ze = new ZipEntry(sourceFile.getName());

ze.setSize(sourceFile.length());

ze.setTime(sourceFile.lastModified());

InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));

int readLen = -1;

return objFile.length();

public long createFileToZip(File sourceFile,File zipFile)throws IOException {

File objFile = zipFile;

* 测试解压缩功能. 将d:\\download\\test.zip连同子目录解压到d:\\temp\\zipout目录下.

public void releaseZipToFile(String sourceZip, String outFileName)

throws IOException{

ZipFile zfile=new ZipFile(sourceZip);

System.out.println(zfile.getName());

Enumeration zList=zfile.entries();

ZipEntry ze=null;

while(zList.hasMoreElements()){

//从ZipFile中得到一个ZipEntry

ze=(ZipEntry)zList.nextElement();

if(ze.isDirectory()){

//以ZipEntry为参数得到一个InputStream,并写到OutputStream中

OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(outFileName, ze.getName())));

InputStream is=new BufferedInputStream(zfile.getInputStream(ze));

int readLen=0;

os.write(buf, 0, readLen);

System.out.println("Extracted: "◆ze.getName());

zfile.close();

* 取得指定目录下的所有文件列表,包括子目录.

* @param baseDir

* File 指定的目录

* @return 包含java.io.File的List

private List getSubFiles(File baseDir) {

List ret = new ArrayList();

//File base=new File(baseDir);

File[] tmp = baseDir.listFiles();

for (int i = 0; i tmp.length; i◆◆) {

if (tmp[i].isFile()) {

ret.add(tmp[i]);

if (tmp[i].isDirectory()) {

ret.addAll(getSubFiles(tmp[i]));

return ret;

* 给定根目录,返回一个相对路径所对应的实际文件名.

* 指定根目录

* @param absFileName

* 相对路径名,来自于ZipEntry中的name

* @return java.io.File 实际的文件

private File getRealFileName(String baseDir, String absFileName) {

String[] dirs = absFileName.split("/");

//System.out.println(dirs.length);

File ret = new File(baseDir);

//System.out.println(ret);

if (dirs.length 1) {

for (int i = 0; i dirs.length - 1; i◆◆) {

ret = new File(ret, dirs[i]);

if (!ret.exists()) {

ret.mkdirs();

ret = new File(ret, dirs[dirs.length - 1]);

* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.

* java.lang.String 根目录

* @param realFileName

* java.io.File 实际的文件名

* @return 相对文件名

private String getAbsFileName(String baseDir, File realFileName) {

File real = realFileName;

File base = new File(baseDir);

String ret = real.getName();

while (true) {

real = real.getParentFile();

if (real == null)

break;

if (real.equals(base))

ret = real.getName() ◆ "/" ◆ ret;

System.out.println("TTTTT" ◆ ret);

public void testReadZip() throws Exception{

String baseDir="d:\\temp\\zipout";

ZipFile zfile=new ZipFile("d:\\download\\src.zip");

OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(baseDir, ze.getName())));

ZipManager manager = new ZipManager();

//manager.releaseZipToFile("c:\\test.zip","c:\\test");

manager.testReadZip();

catch (Exception e) {}

System.out.println("over");

B/S结构的环境中?那最好不要这样设计,,,,使用applet是可以做到,但要求权限放得很低,并且目前的浏览器很麻烦配置.

可以考虑post到服务器端处理

以上就是土嘎嘎小编为大家整理的java读取rar代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章