JAVA认证

Java文件解压缩实例详解

时间:2024-06-20 01:14:56 JAVA认证 我要投稿
  • 相关推荐

Java文件解压缩实例详解2016

  为了帮助广大考试新手们更加顺利地复习备考,小编整理了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!");

  }

  }

【Java文件解压缩实例详解】相关文章:

JAVA认证基础知识:Java文件解压缩示例06-11

Java的压缩与解压缩ZIP11-01

Linux解压缩命令详解201608-11

关于JAVA实现httpClient的实例11-05

Java读取xml文件的方法10-10

JQuery菜单效果实例详解06-09

word 文档域应用实例详解09-20

C盘系统文件作用详解09-08

javascript 单例模式详解及简单实例07-08

JavaScript中push(),join() 函数实例详解05-30