2019年09月18日(星期三)  农历:己亥年八月二十

作者:三年。分类: JAVA 标签: java java基础 java学习 Java入门 java编程思想 java基础学习

Java实现压缩文件的解压缩操作,缺点是压缩文件内不能含有文件名为中文的的文件,否则会出现如下错误:

Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED

at java.util.zip.ZipCoder.toString(Unknown Source)

at java.util.zip.ZipInputStream.readLOC(Unknown Source)

at java.util.zip.ZipInputStream.getNextEntry(Unknown Source)

at com.javatest.techzero.gui.ZipFileDemo.main(ZipFileDemo.java:22)

代码:

package com.javatest.techzero.gui;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;

public class ZipFileDemo {

@SuppressWarnings("resource")

public static void main(String args[]) throws Exception {

File file = new File("d:" + File.separator + "test.zip");

File outFile = null;

ZipFile zipFile = new ZipFile(file);

ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));

ZipEntry entry = null;

InputStream input = null;

OutputStream out = null;

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

System.out.println("开始解压缩" + entry.getName() + "文件。。。");

outFile = new File("d:" + File.separator + entry.getName());

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

outFile.getParentFile().mkdir();

}

if (!outFile.exists()) {

outFile.createNewFile();

}

input = zipFile.getInputStream(entry);

out = new FileOutputStream(outFile);

int temp = 0;

while ((temp = input.read()) != -1) {

//System.out.println(temp);

out.write(temp);

}

input.close();

out.close();

}

System.out.println("Done!");

}

}

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 0/1381 人参与

发表评论:



手Q扫描加入Java初学者群