使用org.apache.tools.zip包来压缩解压缩文件。
java 代码
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
-
- public class CompressFile {
- private static CompressFile instance = new CompressFile();
-
- private CompressFile() {
- }
-
- public static CompressFile getInstance() {
- return instance;
- }
-
- public synchronized void zip(String inputFilename, String zipFilename)
- throws IOException {
- zip(new File(inputFilename), zipFilename);
- }
-
- public synchronized void zip(File inputFile, String zipFilename) throws IOException {
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
- zipFilename));
-
- try {
- zip(inputFile, out, "");
- } catch (IOException e) {
- throw e;
- } finally {
- out.close();
- }
- }
-
- private synchronized void zip(File inputFile, ZipOutputStream out, String base)
- throws IOException {
- if (inputFile.isDirectory()) {
- File[] inputFiles = inputFile.listFiles();
- out.putNextEntry(new ZipEntry(base + "/"));
- base = base.length() == 0 ? "" : base + "/";
- for (int i = 0; i < inputFiles.length; i++) {
- zip(inputFiles[i], out, base + inputFiles[i].getName());
- }
-
- } else {
- if (base.length() > 0) {
- out.putNextEntry(new ZipEntry(base));
- } else {
- out.putNextEntry(new ZipEntry(inputFile.getName()));
- }
-
- FileInputStream in = new FileInputStream(inputFile);
- try {
- int c;
- byte[] by = new byte[BUFFEREDSIZE];
- while ((c = in.read(by)) != -1) {
- out.write(by, 0, c);
- }
- } catch (IOException e) {
- throw e;
- } finally {
- in.close();
- }
- }
- }
-
- public synchronized void unzip(String zipFilename, String outputDirectory)
- throws IOException {
- File outFile = new File(outputDirectory);
- if (!outFile.exists()) {
- outFile.mkdirs();
- }
-
- ZipFile zipFile = new ZipFile(zipFilename);
- Enumeration en = zipFile.getEntries();
- ZipEntry zipEntry = null;
- while (en.hasMoreElements()) {
- zipEntry = (ZipEntry) en.nextElement();
- if (zipEntry.isDirectory()) {
-
- String dirName = zipEntry.getName();
- dirName = dirName.substring(0, dirName.length() - 1);
-
- File f = new File(outFile.getPath() + File.separator + dirName);
- f.mkdirs();
-
- } else {
-
- File f = new File(outFile.getPath() + File.separator
- + zipEntry.getName());
- f.createNewFile();
- InputStream in = zipFile.getInputStream(zipEntry);
- FileOutputStream out = new FileOutputStream(f);
- try {
- int c;
- byte[] by = new byte[BUFFEREDSIZE];
- while ((c = in.read(by)) != -1) {
- out.write(by, 0, c);
- }
-
- } catch (IOException e) {
- throw e;
- } finally {
- out.close();
- in.close();
- }
- }
- }
- }
-
- private static final int BUFFEREDSIZE = 1024;
-
- public static void main(String[] args) {
- CompressFile bean = new CompressFile();
- try {
- bean.zip("d:/temp", "d:/test.zip");
-
- bean.unzip("d:/test.zip", "d:/out/temp");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
分享到:
相关推荐
JAVA文件压缩与解压缩实践(源代码+论文)
java 解压缩zip文件 java 解压缩zip文件 java 解压缩zip文件
(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业...
JAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩...
JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件...
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与...
java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文)....
JAVA文件压缩与解压缩实践报告 主函数 gzip压缩模块代码 压缩模块要完成的就是将文件读入以后进行压缩,再将压缩后的数据写入一个新的文件,其部分代码如下: public class gzip { public static void main(String...
Java实现压缩解压缩文件和文件夹(附源码) zip unzip 压缩 解压缩
java开发文件解压缩工具箱,可解压ZIP,RAR解决中文乱码
用于自动解压缩文件,可用于web服务器上
java 写的一个工具类可以用来解压缩zip,rar文件到指定的文件夹下
JAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA...
java语言操作解压缩文件。 /** * 数据压缩 * * @param data * @return * @throws Exception */ public static byte[] compress(byte[] data) throws Exception { ByteArrayInputStream bais = new ...
java解压 JieyaZipDemo1.java
Java文件压缩与解压缩是指将一个或多个文件或文件夹打包成一个压缩文件,或者将压缩文件解压缩成原始文件的过程。 常见的压缩格式包括ZIP、GZIP、TAR等,Java提供了相应的API来实现对这些格式的操作。 功能特点: ...