zoukankan      html  css  js  c++  java
  • java 打包含有文件夹成zip的问题

    public class test{
    /**
    * @param args
    */
    public static void main(String[] args)
    {
    // TODO Auto-generated method stub
    FileToZip zip = new FileToZip();
    //zip文件压缩
    boolean dook = zip.makeFile_Zip("C://test/testqqqqq.zip","C://test/test/");
    //zip文件解压
    //boolean dook = zip.openFile_Zip("D://test.zip","D://test");
    if(dook == true)
    {
    System.out.println("打包文件成功…");
    //System.out.println("解压zip文件成功…");
    }
    else
    {
    System.out.println("打包文件失败…");
    //System.out.println("解压zip文件失败…");
    }
    }
    }
    class FileToZip
    {
    //=============================================
    // 打包文件入口类
    // parm:生成文件名,要打包的文件名,或目录
    // =============================================
    public boolean makeFile_Zip(String outfile, String infile)
    {
    try
    {
    // 文件输出流
    FileOutputStream fout = new FileOutputStream(outfile);
    // zip文件输出流
    ZipOutputStream out = new ZipOutputStream(fout);
    // 要打包的文件
    File file = new File(infile);
    // 进行打包zip操作,第一次打包不指定文件夹,因为程序接口中指定了一级文件夹
    makeFile_Zip_Do(out, file, "");
    // 关闭zip输出流
    out.close();
    //返回成功
    return true;
    }
    catch(FileNotFoundException e)
    {
    System.out.println("打包失败(指定的文件不存在)…");
    return false;
    }
    catch(Exception e)
    {
    System.out.println("打包失败(未知原因)…");
    return false;
    }
    }
    // =============================================
    // 打包文件操作类
    // parm:zip输出流,打包文件,下一级的目录
    //=============================================
    public void makeFile_Zip_Do(ZipOutputStream out, File file, String dir) throws IOException
    {
    //如果当前打包的是目录
    if (file.isDirectory())
    {
    //输出进度信息
    System.out.println("当前正在打包文件夹:" + file + "…");
    //文件列表
    File[] files = file.listFiles();
    //添加下一个打包目录文件
    //out.putNextEntry(new ZipEntry(dir + "/"));
    //
    dir = dir.length() == 0 ? "" : dir + "/";
    for (int i = 0; i < files.length; i++) {
    makeFile_Zip_Do(out, files[i], dir + files[i].getName());
    }
    }
    //如果当前打包文件
    else
    {
    //输出进度信息
    System.out.println("当前正在打包文件:" + file +" "+ "…");
    //
    
    out.putNextEntry(new ZipEntry(dir + file.getName()));
    
    System.out.println(file.getName());
    //文件输入流
    FileInputStream in = new FileInputStream(file);
    System.out.println(file);
    int i;
    //进行写入
    while ((i = in.read()) != -1) {
    out.write(i);
    
    }
    //关闭输入流
    in.close();
    }
    }
    }

    可以压缩成zip 的格式但是我自己去手动解压的时候发现压缩的文件名重复了,比如压缩一个testcase.doc ,我手动解压之后文件名称变成testcase.doctestcase.doc 不知道程序哪个地方有问题,请高手帮我看看谢谢

  • 相关阅读:
    Building Java Projects with Gradle
    Vert.x简介
    Spring及Spring Boot 国内快速开发框架
    dip vs di vs ioc
    Tools (StExBar vs Cmder)which can switch to command line window on context menu in windows OS
    SSO的定义、原理、组件及应用
    ModSecurity is an open source, cross-platform web application firewall (WAF) module.
    TDD中测试替身学习总结
    Spring事务银行转账示例
    台式机(华硕主板)前面板音频接口(耳机和麦克风)均无声的解决办法
  • 原文地址:https://www.cnblogs.com/lcuzhanglei/p/2990000.html
Copyright © 2011-2022 走看看