是否有任何java压缩实用程序 [英] Is there any java compression utility

查看:134
本文介绍了是否有任何java压缩实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要(解压缩)一些文件,一些谷歌后,我还没有找到任何实用程序。我知道我可以做它与内置的java.uitl.zip。*类,但没有任何实用程序,这将使它更容易,最好是这样:

I need to (un)zip some files and after some googling I haven't found any utility for doing it. I know I can do it with built-in java.uitl.zip.* classes but isn't there any utility, that will make it easer, best like this:

SomeClass.unzip(file_name.zip,target_directory);

SomeClass.unzip("file_name.zip", "target_directory");

SomeClass.zip (source_directory,target_file_name.zip,递归);

SomeClass.zip("source_directory", "target_file_name.zip", recursively);

我不想处理流。只是文件,或更好只是文件名...

I don't want to handle streams. Just file, or better just file names...

推荐答案

Deflater / Inflater 类://stackoverflow.com/questions/127001/whats-a-good-compression-library-for-java>什么是Java的好压缩库?。

How about the Deflater/Inflater classes mentioned in the question "What’s a good compression library for Java?".

我知道Java提出的当前接口是基于流的,而不是文件名,但是根据 Java压缩,很容易构建一个这样的实用程序类:

I know the current interfaces proposed by Java are Stream-based, not "filename"-based, but according to the following article on Java compression, it is easy enough to build an utility class around that:

基于文件名的解压缩函数将如下所示:

For instance, a Unzip function based on a filename would look like:

import java.io.*;
import java.util.zip.*;

public class UnZip {
   final int BUFFER = 2048;
   public static void main (String argv[]) {
      try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
       FileInputStream(argv[0]);
         ZipInputStream zis = new 
       ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new 
          FileOutputStream(entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

这篇关于是否有任何java压缩实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆