`
neil.zou
  • 浏览: 12798 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

java zip and unzip

    博客分类:
  • Java
阅读更多
jdk提供了Zip相关的类方便的实现压缩和解压缩。使用方法很简单。下边分别是压缩和解压缩的简单事例
1,压缩的
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {
    static final int BUFFER = 2048;

    public static void main(String argv[]) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream("E:\\test\\myfiles.zip");
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];
            File f = new File("e:\\test\\a\\");
            File files[] = f.listFiles();

            for (int i = 0; i < files.length; i++) {
                FileInputStream fi = new FileInputStream(files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(files[i].getName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2,解压缩的。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UnZip {
    static final int BUFFER = 2048;

    public static void main(String argv[]) {
        try {
            String fileName = "E:\\test\\myfiles.zip";
            String filePath = "E:\\test\\";
            ZipFile zipFile = new ZipFile(fileName);
            Enumeration emu = zipFile.entries();
            int i=0;
            while(emu.hasMoreElements()){
                ZipEntry entry = (ZipEntry)emu.nextElement();
                //会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
                if (entry.isDirectory())
                {
                    new File(filePath + entry.getName()).mkdirs();
                    continue;
                }
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                File file = new File(filePath + entry.getName());
                //加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
                //而这个文件所在的目录还没有出现过,所以要建出目录来。
                File parent = file.getParentFile();
                if(parent != null && (!parent.exists())){
                    parent.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);          
               
                int count;
                byte data[] = new byte[BUFFER];
                while ((count = bis.read(data, 0, BUFFER)) != -1)
                {
                    bos.write(data, 0, count);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
            zipFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
PS:

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
改成
import org.apache.tools.zip.*;

然后把
Enumeration emu = zipFile.entries();
改成
Enumeration emu = zipFile.getEntries();
就可以支持中文了
分享到:
评论

相关推荐

    java-ZIP-AND-UNZIP.zip_zip

    在Java编程语言中,处理ZIP压缩和解压缩是常见的任务,尤其在文件管理和数据传输方面。ZIP是一种广泛使用的文件格式,用于将多个文件打包到一个单一的可移植档案中,以便于存储、传输或备份。以下是一些关于Java中...

    Linux系统unzip解压后中文名乱码解决方法.docx

    解决 Linux 系统 unzip 解压后中文名乱码的问题可以使用四种方法:使用 Python 脚本、使用 unzip 命令指定字符集、在环境变量中指定 unzip 参数、使用 Java 的 jar 命令解压 zip 文件。每种方法都可以解决中文名乱码...

    java反编工具java反编工具

    Unzip djdec299.zip file into any appropriate directory on your hard drive. This will create file SetupDJ.exe. To install or re-install DJ Java Decompiler run SetupDJ.exe To run DJ Java Decompiler ...

    chart java applet

    In order to run the example application unzip the file .zip and read the HowToRun.txt file. FILES: Examples.htm: applet examples - Javadoc directory: javadoc files. - Help_english....

    jd-eclipse-2.0.0.zip

    Build or download & unzip "jd-eclipse-site-x.y.z.zip", Launch Eclipse, Click on "Help &gt; Install New Software...", Click on button "Add..." to add an new repository, Enter "JD-Eclipse Update Site" ...

    jddb.zip_The First_breakwindow_jddb_reflect Copy

    Download the Java Debugger and unzip it into its own directory (eg: d:jddb) Add d:jddb to your path, or copy d:jddbjddb.cmd into your path. Modify the first 3 lines of jddb.cmd to reflect the ...

    opencv3.4.4 and contrib.zip

    1. 解压下载的zip文件:`unzip opencv3.4.4_and_contrib.zip` 2. 创建构建目录并进入:`mkdir build && cd build` 3. 使用CMake配置构建过程,指定源代码目录和安装目标目录:`cmake -D CMAKE_BUILD_TYPE=Release -D...

    Java邮件开发Fundamentals of the JavaMail API

    A general familiarity with object-oriented programming concepts and the Java programming language is necessary. The Java language essentials tutorial can help. copyright 1996-2000 Magelang ...

    jdk-8u201-linux-x64

    Java SE Development Kit 8 Downloads Thank you for downloading this release of the Java™ Platform, Standard Edition ...1 unzip jdk-8u201-linux-x64.tar.gz.zip 2 tar -zxvf jdk-8u201-linux-x64.tar.gz

    Android代码-glowroot

    Download and unzip glowroot-0.13.0-dist.zip Add -javaagent:path/to/glowroot.jar to your application's JVM args [where are my application server's JVM args?] Check that the user account that runs the ...

    安装oracle-19c依赖base.zip

    1. **解压压缩包**:使用`unzip base.zip`命令将所有文件解压到指定目录。 2. **安装依赖**:根据提供的脚本或指南,逐个安装所需依赖,如`yum install -y libaio libaio-devel`等。 3. **配置环境**:设置必要的...

    jdk1.8.zip

    - **Date and Time API**:全新的java.time包取代了过时的java.util.Date和Calendar,提供了更强大和易用的时间日期处理。 总的来说,JDK 1.8在保持向后兼容的同时,引入了许多现代化的编程概念,提升了开发效率和...

    com.cb.eclipse.folding.KAM-3.5.zip Eclipse代码折叠插件

    说明: Eclipse代码折叠插件 用过 Visual Studio 的朋友都知道 #region #endregion 可以将一...Then use Windows &gt; Preferences &gt; Java &gt; Editor &gt; Folding and specify user defined regions 亲测Eclipse Juno可用

    bmcrbcomposition.rar_网络编程_Java_

    然后,我们看到压缩包子文件的文件名称列表,其中有“ZipAndUnzip.jpx”及其不同版本和备份文件,这很可能是一个实现了文件压缩和解压缩功能的Java程序。在Java中,我们可以使用`java.util.zip`包来处理ZIP文件。`...

    com.sysdeo.eclipse.tomcat_3.2.1

    To set a JDK as default JRE for Eclipse open the preference window : Window -&gt; Preferences -&gt; Java -&gt; Installed JREs. This JRE must be a JDK (This is a Tomcat prerequisite). The plugin sets itself ...

    Java反编译软件JAD1

    Unzip jad.zip file into any appropriate directory on your hard drive. This will create two files: - an executable file named 'jad.exe' (Windows 95/NT) or 'jad' (Linux) - this README file No ...

    tigase-server-8.0.0-b10083-dist-max.zip

    2. **解压文件**:使用`unzip`命令将下载的“tigase-server-8.0.0-b10083-dist-max.zip”解压到一个合适的目录,例如 `/opt/tigase`。 3. **配置Tigase**:进入解压后的目录,找到并编辑配置文件,如 `tigase.conf`...

    opencv_contrib-3.4.7.zip

    3. 解压并进入目录:`unzip opencv-3.4.7.zip && unzip opencv_contrib-3.4.7.zip && cd opencv-3.4.7` 4. 配置CMake:`mkdir build && cd build && cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/...

Global site tag (gtag.js) - Google Analytics