Java中的文件压缩

Java中的文件压缩
 个人写的一个压缩文件或目录的程序。对于目录中的文件,使用dir//dir//filename.txt格式即可。
该程序不支持中文,下面引用了网上的一种解决方案。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 压缩(多个)文件或目录,暂不支持中文
 * @author huanyun
 *
 */
public class ZipCompressor {    /**     * 压缩多个文件和目录     * @param filenames     * @param destFile
     * @throws IOException
     */
    public static void compress(String[] filenames, String destFile) throws IOException {       
        FileOutputStream fos = new FileOutputStream(destFile);
        // 使用CRC32进行校验
        CheckedOutputStream csum = new CheckedOutputStream(fos, new CRC32());
        ZipOutputStream zOut = new ZipOutputStream(csum);
        zOut.setComment("This is a jar file.");
        for(String filename:filenames){
            File file = new File(filename);
            if (!file.exists())
                continue;
            if(file.isDirectory()){
                String baseName = file.getName() + File.separator;
                rescursionCompress(zOut, file, baseName);
            }else{
                compressFile(zOut,new FileInputStream(file),file.getName());
            }
        }       
        zOut.close();
    }
   
    /**
     * 压缩单个文件或者目录
     * @param filename
     * @param destFile
     * @throws IOException
     */
    public static void compress(String filename, String destFile)
            throws IOException {
        compress(new String[]{filename},destFile);
    }

    /**
     * 递归的写入文件
     * @param zOut
     * @param dir
     * @param baseName
     * @throws IOException
     */
    private static void rescursionCompress(ZipOutputStream zOut, File dir,
            String baseName) throws IOException {
        File[] files = dir.listFiles();
        for (File f : files) {
            if (f.isHidden())
                continue;
            if (f.isDirectory()) {
                String newBaseName = baseName + f.getName() + File.separator;
                rescursionCompress(zOut, f, newBaseName);
            } else {
                String entryName = baseName + f.getName();
                InputStream in = new FileInputStream(f);
                compressFile(zOut, in, entryName);
                in.close();
            }
        }

    }

    /**
     * 压缩单个的文件
     *
     * @param zOut
     *            压缩输出流
     * @param filename
     *            被压缩的文件名称
     * @param relativedName
     *            在压缩文件中存在的名称
     * @throws IOException
     */
    public static void compressFile(ZipOutputStream zOut, InputStream in,
            String relativedName) throws IOException {
        ZipEntry entry = new ZipEntry(relativedName);
        zOut.putNextEntry(entry);
        int c = -1;
        while ((c = in.read()) != -1)
            zOut.write(c);
        in.close();
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        String dir = "f://testdir";
        String filename = "f://new.zip";
        compress(new String[]{"f://testdir","f://Eclipse.txt","f://pic"}, filename);
    }

}


中文的解决办法:

之前介绍了利用java.util.zip这个package里的class来完成压缩及解压缩的 工作,但是我们知道,java对於文字的编码是以unicode为基础,因此,若是以ZipInputStreamZipOutputStream来处 理压缩及解压缩的工作,碰到中文档名或路径,那当然是以unicode来处理罗!
但是,现在市面上的压缩及解压缩软体,例如winzip,却是不支援unicode的,一碰到档名以unicode编码的档案,它就不处理。
要如何才能做出让winzip能够处理的压缩档呢?那就得从修改ZipInputStreamZipOutputStream对於档名的编码方式来着手 了。我们可以从jdksrc.zip取得ZipInputStreamZipOutputStream的原始码来加以修改:

一、ZipOutputStream.java
1.
jdksrc.zip取得ZipOutputStream.java原始码,另存新档存到c:javautilzip这个资料夹里,档名改为CZipOutputStream.java
2.
开始修改原始码,将class名称改为CZipOutputStream
3.
建构式也必须更改为CZipOutputStream
4.
新增member,这个member记录编码方式
private String encoding="UTF-8";
5.
再新增一个建构式(这个建构式可以让这个classnew的时候,设定档名的编码)
public CZipOutputStream(OutputStream out,String encoding) {
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
usesDefaultDeflater = true;
this.encoding=encoding;
}
6.
找到byte[] nameBytes = getUTF8Bytes(e.name);(有二个地方),将它修改如下:
byte[] nameBytes = null;
try
{
if (this.encoding.toUpperCase().equals("UTF-8"))
nameBytes =getUTF8Bytes(e.name);
else
nameBytes= e.name.getBytes(this.encoding);
}
catch(Exception byteE)
{
nameBytes=getUTF8Bytes(e.name);
}
7.
将档案储存在c:javautilzip这个资料夹内,请记得一定要有这个路径结构,
才能把CZipOutputStream.class放在正确的package结构里


二、ZipInputStream.java
1.
jdksrc.zip取得ZipInputStream.java原始码,另存新档存到c:javautilzip这个资料夹里,档名改为CZipInputStream.java
2.
开始修改原始码,将class名称改为CZipInputStream
3.
建构式也必须更改为CZipInputStream
4.
新增member,这个member记录编码方式
private String encoding="UTF-8";
5.
再新增一个建构式如下(这个建构式可以让这个classnew的时候,设定档名的编码)
public CZipInputStream(InputStream in,String encoding) {
super(new PushbackInputStream(in,512),new Inflater(true),512);
usesDefaultInflater = true;
if(in == null) {
throw new NullPointerException("in is null");
}
this.encoding=encoding;
}

6.
找到ZipEntry e = createZipEntry(getUTF8String(b, 0, len));这一行,将它改成如下:
ZipEntry e=null;
try
{
if (this.encoding.toUpperCase().equals("UTF-8"))
e=createZipEntry(getUTF8String(b, 0, len));
else
e=createZipEntry(new String(b,0,len,this.encoding));
}
catch(Exception byteE)
{
e=createZipEntry(getUTF8String(b, 0, len));
}
7.
将档案储存在c:javautilzip这个资料夹内,请记得一定要有这个路径结构,才能把CZipInputStream.class放在正确的package结构里


上两个档案储存後compile产生CZipOutputStream.classCZipInputStream.class,使用winzip开启 [java_home]jrelibt.jar这个档案,将CZipOutputStream.classCZipInputStream.class 加进去,记得「Save full path info」一定要打勾。
以後当压缩及解压缩时有中文档名及路径的问题时,就可以指定编码方式来处理了。


CZipOutputStream zos=new CZipOutputStream(OutputStream os,String encoding);
CZipInputStream zins=new CZipInputStream(InputStream ins,String encoding);
以「压缩与解压缩(1)」为例:
FileOutputStream fos =new FileOutputStream(request.getRealPath("/")+"myzip.zip");
CZipOutputStream zos=new CZipOutputStream(fos,"BIG5");
其他地方都不用改,便可以处理中文档名的压缩。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值