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

Python012

如何在java中解压zip和rar文件,第1张

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来读取UOS格式的国产文档的话,是有API可以来读取的,你可以通过Maven仓库来在下载spire.xls.jar, 在pom.xml中配置依赖:

<repositories>

<repository>

<id>com.e-iceblue</id>

<name>e-iceblue</name>

<url>

</repository></repositories>

<dependencies>

<dependency>

<groupId>e-iceblue </groupId>

<artifactId>spire.xls</artifactId>

<version>12.7.4</version>

</dependency>

</dependencies>

不好意思搞反了,这样就更简单了。

用这个构造方法ZipInputStream(InputStream in)接收传过来的流,然后用这个类的getNextEntry()方法解压缩文件,最后调用read(byte[] b, int off, int len)方法将数据写入byte数组。

ZipInputStream zin = new ZipInputStream(in)

ZipEntry entry = null

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

if(entry.isDirectory()||entry.getName().equals("..\\"))

continue

BufferedInputStream bin = new BufferedInputStream(zin)

byte[] buf = new byte[]

bin.read(buf,0,1)

}