「java自动解压缩」压缩文件自动解压

博主:adminadmin 2023-01-14 13:24:16 305

今天给各位分享java自动解压缩的知识,其中也会对压缩文件自动解压进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何解决java程序解压含有中文名的zip压缩包出现乱码

上次利用java自动的java.util.zip.ZipEntry和�0�2java.util.zip.ZipFile来解压zip文件,今天发现程序在读取解压文件时居然报了空指针异常,debug程序后发现时读取不到文件,产生原先是zip压缩文件中含有中文的名称,读取文件名为乱码,

报找不到文件名,所以报了空指针,想到ant构建文件也有这个功能,换了apache的ant.jar居然解决了中文的问题。

备份下。

�0�2import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;/*** 读取zip压缩文件中文本的内容

* @author fish*/public class ReadZip {

public static void main(String args[]) {try {String fileName = "D:/workspace/java/src/ReadZip.zip";

//构造ZipFile

ZipFile zf = new ZipFile(new File(fileName));

//返回 ZIP file entries的枚举.

Enumeration? extends ZipEntry entries = zf.getEntries();

while (entries.hasMoreElements()) {

ZipEntry ze = entries.nextElement();

System.out.println("name:"+ze.getName());

long size = ze.getSize();

if (size 0) {

System.out.println("Length is " + size);

BufferedReader br = new BufferedReader(

new InputStreamReader(zf.getInputStream(ze)));

String line;

while ((line = br.readLine()) != null) {

有哪些软件可以解压缩JAVA格式的文件?

用rar软件打开jar文件,把A拖动到已打开的jar文件中即可,注意路径啊!

你好,最近我也遇到用java压缩和解压,向你咨询下你的解决方案什么,你是怎么用文件流方式去压缩?

package com.onewaveinc.cwds.commons.utils;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* @author fz 2010-7-30

* @Description 把指定文件夹下的所有文件压缩为指定文件夹下指定zip文件;把指定文件夹下的zip文件解压到指定目录下

*/

public class ZipUtils {

private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);

private static final String SEPARATE = "/";

/**

* @Author fz 2010-7-30

* @param sourceDir 待压缩目录

* @param zipFile 压缩文件名称

* @throws Exception

* @Description 把sourceDir目录下的所有文件进行zip格式的压缩,保存为指定zip文件

*/

public static void zip(String sourceDir, String zipFile) throws Exception {

OutputStream os = null;

// try {

os = new FileOutputStream(zipFile);

BufferedOutputStream bos = new BufferedOutputStream(os);

ZipOutputStream zos = new ZipOutputStream(bos);

File file = new File(sourceDir);

String basePath = null;

if (file.isDirectory()) {

basePath = file.getPath();

} else {

// 直接压缩单个文件时,取父目录

basePath = file.getParent();

}

zipFile(file, basePath, zos);

zos.closeEntry();

zos.close();

// } catch (Exception e) {

// logger.error("压缩文件或文件夹" + sourceDir + "时发生异常");

// e.printStackTrace();

// }

}

/**

* @Author fz 2010-7-30

* @param source 源文件

* @param basePath 待压缩文件根目录

* @param zos 文件压缩流

* @Description 执行文件压缩成zip文件

*/

private static void zipFile(File source, String basePath, ZipOutputStream zos) {

File[] files = new File[0];

if (source.isDirectory()) {

files = source.listFiles();

} else {

files = new File[1];

files[0] = source;

}

//存相对路径(相对于待压缩的根目录)

String pathName = null;

byte[] buf = new byte[1024];

int length = 0;

try {

for (File file : files) {

if (file.isDirectory()) {

pathName = file.getPath().substring(basePath.length() + 1) + SEPARATE;

zos.putNextEntry(new ZipEntry(pathName));

zipFile(file, basePath, zos);

} else {

pathName = file.getPath().substring(basePath.length() + 1);

InputStream is = new FileInputStream(file);

BufferedInputStream bis = new BufferedInputStream(is);

zos.putNextEntry(new ZipEntry(pathName));

while ((length = bis.read(buf)) 0) {

zos.write(buf, 0, length);

}

is.close();

}

}

} catch (Exception e) {

logger.error("压缩文件" + source + "时发生异常");

e.printStackTrace();

}

}

/**

* @Author fz 2010-7-30

* @param zipfile 待解压文件

* @param destDir 解压文件存储目录

* @throws Exception

* @Description 解压zip文件,只能解压zip文件

*/

@SuppressWarnings("unchecked")

public static void unZip(String zipfile, String destDir) throws Exception {

destDir = destDir.endsWith(SEPARATE) ? destDir : destDir + SEPARATE;

byte b[] = new byte[1024];

int length;

ZipFile zipFile;

// try {

zipFile = new ZipFile(new File(zipfile));

Enumeration enumeration = zipFile.getEntries();

ZipEntry zipEntry = null;

while (enumeration.hasMoreElements()) {

zipEntry = (ZipEntry) enumeration.nextElement();

File loadFile = new File(destDir + zipEntry.getName());

if (zipEntry.isDirectory()) {

loadFile.mkdirs();

} else {

if (!loadFile.getParentFile().exists()) {

loadFile.getParentFile().mkdirs();

}

OutputStream outputStream = new FileOutputStream(loadFile);

InputStream inputStream = zipFile.getInputStream(zipEntry);

while ((length = inputStream.read(b)) 0)

outputStream.write(b, 0, length);

outputStream.close();

inputStream.close();

}

}

zipFile.close();

// } catch (IOException e) {

// logger.error("解压文件" + zipfile + "时发生异常");

// e.printStackTrace();

// }

}

}

java自动解压缩的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于压缩文件自动解压、java自动解压缩的信息别忘了在本站进行查找喔。