Java解压缩文件夹

Java压缩和解压缩
placeholder image
admin 发布于:2015-06-06 02:00:54
阅读:loading

在展示某些内容的时候,如word、excel、ppt等office文件,怎么展示呢?当然采取的是转换为html网页的形式来展示的,word、excel文件里面都是可以写入超链接、插入图片等功能,转换后网页看起来也比较好看,但是在上传这些资料的时候这些office文件会产生一个和文件同名的.files文件,这时候我们上传文件的时候就需要将这个文件达成一个包然后再上传到服务器上,程序再解压这个文件,获取文件里面的.html文件来显示,这些不再赘述,想说的是程序来解压文件夹,解压的是zip类型的压缩文件,并且这里的程序只能是解压非中文类型的文件名字,因为有中文命名的后,解压会出现乱码的错误。

程序如下:

package com;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class UNZipFile {
    public String getRealPath(String path) {
        int len = path.lastIndexOf(".");
        if (len != -1) {
            return path.substring(0, len);
        }
        return null;
    }
    public void unzipTest() {
        String filePath = "e:\\temp\\tt.zip";
        String rootPath = getRealPath(filePath);
        File file = new File(rootPath);
        String parentFile = file.getParent();
        try {
            ZipFile zip = new ZipFile(filePath);
            Enumeration enums = zip.entries();
            while (enums.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) enums.nextElement();
                if (entry.isDirectory()) {
                    System.out.println(entry.getName() + "   文件夹名称!\t"
                            + rootPath + "\\" + entry.getName());
                    new File(parentFile + "\\" + entry.getName()).mkdir();
                    System.out.println("文件夹创建成功!");
                    continue;
                }
                BufferedInputStream bufferis = new BufferedInputStream(
                        zip.getInputStream(entry));
                BufferedOutputStream bufferos = new BufferedOutputStream(
                        new FileOutputStream(parentFile + "\\"
                                + entry.getName()));
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = bufferis.read(b)) != -1) {
                    bufferos.write(b, 0, len);
                }
                bufferos.close();
                bufferis.close();
            }
            System.out.println("文件写入完毕!");
        } catch (IOException e) {
            System.err.println("没有找到文件!");
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        UNZipFile dao = new UNZipFile();
        dao.unzipTest();
    }
}

=============================================OK

可以解压了,默认的情况就是 D:\\1.zip文件,解压后的文件夹放置在当前目录,相当于 WinRAR解压的“解压到当前文件夹”功能。

还得找找问什么不能解压带中文汉字的文件。

 

 

注:以前老在想压缩包里面不能有中文字符,现在想想,这个功能一般用在于,用PPT之类的文件作的源文件,转换后生成的.html文件和.files文件夹,或者PPT里面有链接到某个文件之类的,既然是这样需要到浏览器里面点击请求这个文件,就别含有中文的字符,这样解释还是比较好的。

 

补充下:最近在听一个视频教程的时候,讲到了,说rar文件是有注册过专利版权的,所以JDK不提供开源的压缩和解压rar文件的api。

上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:hhttps://www.chendd.cn/information/viewInformation/other/64.a

最后更新:2015-06-06 02:00:54

访问次数:85

评论次数:0

点赞个数:0

 点赞


 发表评论

当前回复:作者

 评论列表


留言区