`

UnZIP folder

阅读更多
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class UnZIPcontrol {

/**
  * 创建目录
  *
  * @param path
  *            目录绝对路径名
  */
static void createDir(String path) {
  File dir = new File(path);
  if (dir.exists() == false)
   dir.mkdir();
}

/**
  * 取得文件名,不包含后缀名
  *
  * @param name
  *            完整文件名
  * @return 文件名(不包含后缀名)
  */
static String getSuffixName(String name) {
  return name.substring(0, name.lastIndexOf("."));
}

public static void main(String[] args) throws Exception {
//  unzip("E:/apache-tomcat-6.0.29.zip", "E:");

unzip("E:/test005.zip", "E:");

  System.out.println("over....................");
}

/**
  * 解压zip文件
  *
  * @param zipFilePath
  *            zip文件绝对路径
  * @param unzipDirectory
  *            解压到的确
  * @throws Exception
  */
public static String unzip(String zipFilePath, String unzipDirectory)
   throws Exception {
  // 创建文件对象
  File file = new File(zipFilePath);
  // 创建zip文件对象
  ZipFile zipFile = new ZipFile(file);
  // 创建本zip文件解压目录
  File unzipFile = new File(unzipDirectory + "/"
    + getSuffixName(file.getName()));
  if (unzipFile.exists())
   unzipFile.delete();
  unzipFile.mkdir();
  // 得到zip文件条目枚举对象
  Enumeration zipEnum = zipFile.getEntries();
  // 定义输入输出流对象
  InputStream input = null;
  OutputStream output = null;
  // 定义对象
  ZipEntry entry = null;
  // 循环读取条目
  while (zipEnum.hasMoreElements()) {
   // 得到当前条目
   entry = (ZipEntry) zipEnum.nextElement();
   String entryName = new String(entry.getName());
   // 用/分隔条目名称
   String names[] = entryName.split("\\/");
   int length = names.length;
   String path = unzipFile.getAbsolutePath();
   for (int v = 0; v < length; v++) {
    if (v < length - 1) { // 最后一个目录之前的目录
     path += "/" + names[v] + "/";
     createDir(path);
    } else { // 最后一个
     if (entryName.endsWith("/")) // 为目录,则创建文件夹
      createDir(unzipFile.getAbsolutePath() + "/" + entryName);
     else { // 为文件,则输出到文件
      input = zipFile.getInputStream(entry);
      output = new FileOutputStream(new File(unzipFile
        .getAbsolutePath()
        + "/" + entryName));
      byte[] buffer = new byte[1024 * 8];
      int readLen = 0;
      while ((readLen = input.read(buffer, 0, 1024 *) != -1)
       output.write(buffer, 0, readLen);
      // 关闭流
      input.close();
      output.flush();
      output.close();
     
     }
    }
   }
  }
  return unzipFile.getName();
}
}
分享到:
评论

相关推荐

    windows zip unzip

    在Windows操作系统中,"zip"和"unzip"是指对文件进行压缩和解压缩的操作。Zip是一种常用的文件格式,用于将多个文件或文件夹打包成一个单一的档案,以节省存储空间和便于传输。在Windows中,有多种方法可以处理zip...

    Actionsctipt zip和unzip

    var sourceFiles:Array = [File.applicationDirectory.resolvePath("file1.txt"), File.applicationDirectory.resolvePath("folder1")]; var zipFile:File = File.applicationDirectory.resolvePath("compressed_...

    Windows命令行压缩解压缩工具

    例如,`unzip file.zip -d destination_folder`将把zip文件中的内容解压到destination_folder目录下。 3. **tar**:tar最初是用于在Unix系统中打包文件,但它并不直接进行压缩。`tar`命令常与其他压缩工具结合使用...

    批量解压缩 2005

    通常我们解压缩一个Zip,最简单的方法就是直接在这个Zip文件上点击鼠标右键,然后选择 Extract To folder 将这个Zip文件解压到目录中。 批量解压缩(Batch UnZip)也可以结合到Windows右键菜单—— 先选中要解压...

    ML函数式程序设计包

    ML语言基本的函数模块,递归生成,folder函数实现,map函数,zip,unzip,对列表进行操作

    unzip file\s:MATLAB 解压缩的扩展,它允许提取一个或多个指定的文件...-matlab开发

    MATLAB的标准函数`unzip`能够帮助用户方便地完成这一操作。然而,标准的`unzip`函数会将整个压缩文件解压到指定的目录,而不会提供选择性解压的选项。针对这种情况,"unzip file\s"的功能扩展了MATLAB的解压缩能力,...

    matlab_评估时间分辨GIWAXS数据。它根据GIWAXS图案的极角chi切割q形切割

    Then, unzip GIXSGUI folder. Start Matlab. On the Home tab, click "Set Path" &gt; "Add with Subfolders..." and select the folder that contains the GIXSGUI distribution. Click "Save" and "Close".

    U盘启动盘制作工具 U盘启动盘制作工具 U盘启动盘制作工具

    1.Use WinRAR or 7-ZIP to unzip HPUSBFW.EXE to folder "HPUSBFW" There will be 8 files inside this folder. 2.Execute HPUSBFW.EXE 3.Select the USB device you want to format. 4.Check "Create a DOS ...

    WildMagic4.8最新游戏引擎源代码Game Engine 文档

    the top-level folder to any location you prefer on your hard drive. The projects all use relative paths and do not rely on the top-level directory being located at the root of the drive. The Macintosh...

    Dicoman 4.8.6 Build 1172 Dicoman最新版

    You simply download the package and unzip it to a folder. If that did not work out, you may have to install needed runtimes. Download and install Microsoft ActiveX End-User Runtimes (March 2008 or ...

    WildMagic4.8最新游戏引擎源代码(内含Game Engine 安装文档)(1)

    the top-level folder to any location you prefer on your hard drive. The projects all use relative paths and do not rely on the top-level directory being located at the root of the drive. The Macintosh...

    基于pytorch卷积神经网络的中文手写汉字识别,使用HWDB数据库

    Download HWDB dataset and unzip to data folder run python process_gnt.py to generate img from gnt fiel. Due to the huge dataset (897758+223991 images), it may take a lot of time. I suggest to put the ...

    VC++ MFC 如何用 CZip CUnzip类压缩解压缩文件

    例如`myUnzip.Extract("target_path_in_zip\\source_file.txt", "destination_folder\\source_file.txt");`。 4. **获取ZIP信息:** 可以使用`GetFileCount`、`GetFileInfo`等函数获取ZIP文件的信息,例如文件数量...

    linux仿win10主题

    linux仿win10主题,Win10 Icons Pack - GNOME Icon Theme Description: I found this icon pack by ...4) copy the folder pack from ( home / username / ..(folder pack).. ) and paste it in (usr / share / icons)

    zencart156.rar 英文版

    1. Extract the Zen Cart ZIP file (and tell the unzip utility to *retain* folder structures) 2. Upload the Zen Cart files and folders to your server using your FTP program 3. You must make some folders...

    linux常用命令及语法

    例如,`mv file1 file2` 将 `file1` 重命名为 `file2`,`mv folder1 folder2/newfolder` 移动 `folder1` 到 `folder2` 目录下并命名为 `newfolder`。 4. `du` 和 `df`: - `du` 用于查看目录占用的磁盘空间。例如...

    问题完美解决 keil官网方法encounter an inmproper argument

    标题中的“问题完美解决 keil官网方法encounter an inmproper argument”指的是在使用MDK5(Microcontroller Development Kit)时遇到的一个特定错误:“encounter an improper argument”。这个错误通常出现在用户...

    zencart 1.5.1 中文版

    1. Extract the Zen Cart ZIP file (and tell the unzip utility to *retain* folder structures) 2. Upload the Zen Cart files and folders to your server using your FTP program 3. You must make some folders...

    VB生成"正宗"DLL的一个不错的插件

    0)Unzip the zip file -- Remember to extract paths! 1)Open 'Linker.vbp' (in the 'linker' folder) and compile it. 2)Copy the 'MakeDLL.exe' file (in the 'compiled' folder) to your Visual Basic ...

    matlab直接调用R语言

    s toolbox folder or whatever folder you want Be sure to add MATLAB RLINK to your Matlab path 4 Use template and function In the content of matlab2r m there&quot;s a line: matlab2r &quot;y x^2&quot; ...

Global site tag (gtag.js) - Google Analytics