| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1283 人关注过本帖
标题:压缩文件代码
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
压缩文件代码
import java.awt.*;
import *;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.*;

import com.sun.nio.zipfs.ZipDirectoryStream;
public class Example{
    private void zip(String zipFileName,File inputFile)throws Exception{
    ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
    zip(out,inputFile,"");
    System.out.println("压缩中...");
    out.close();
    }

private void zip(ZipDirectoryStream out,File f,String base)
        throws Exception{
    if(f.isDirectory()){
        File[] fl=f.listFiles();
        if(base.length()!=0){
            out.putNextEntry(new ZipEntry(base +"/"));
        }
        for(int i=0;i<fl.length;i++){
            zip(out,fl[i],base+fl[i]);
        }
    }else{
        out.putNextEntry(new ZipEntry(base));
        FileInputStream in =new FileInputStream(f);
        int b;
        System.out.println(base);
        while((b=in.read())!=-1){
            out.write(b);
        }
        in.close();
}
}
    public static void main(String[] temp){
        Example book =new Example();
        try{
            book.zip("E:/hello.zip",new File("E:/hello"));
            System.out.println("压缩完成");
        }catch(Exception ex){
            
        }
    }
}   

有问题,问题代码如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method zip(ZipDirectoryStream, File, String) in the type Example is not applicable for the arguments (ZipOutputStream, File, String)

    at Example.zip(Example.java:12)
    at Example.main(Example.java:41)
怎么改好
搜索更多相关主题的帖子: java zip out Example new 
2018-03-17 15:42
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:20 
程序代码:
package com.xiaoa.demo;

import import import import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Example {
    private void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, inputFile, "");
        System.out.println("压缩中...");
        out.close();
    }

    private void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            if (base.length() != 0) {
                out.putNextEntry(new ZipEntry(base + "/"));
            }
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i]);
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b;
            System.out.println(base);
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
        }
    }

    public static void main(String[] temp) {
        Example book = new Example();
        try {
            book.zip("E:/hello.zip", new File("E:/hello"));
            System.out.println("压缩完成");
        } catch (Exception ex) {

        }
    }
}

假如人生没有梦想,和咸鱼有什么区别!
2018-03-17 18:39
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
回复 2楼 疯狂的小a
哪里错了,能标注一下吗?
2018-03-17 19:27
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
结贴,知道了。
2018-03-17 19:43
快速回复:压缩文件代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018754 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved