Zip文件生成器

package Zip文件生成器17;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCreator {
    public static void main(String args[]){
        System.out.println("请输入生成后zip文件的地址以及zip文件名");
        Scanner sc=new Scanner(System.in);
        String target=sc.next();
        String name=sc.next();
        System.out.println("请输入需要压缩的文件地址");
        String file=sc.next();
        try {
            pressedFile(file,target,name);
            System.out.println("文件压缩成功......");
        }catch(Exception e){
           System.out.println("文件压缩失败......");
           e.printStackTrace();
        }
        /*
        System.out.println("请输入生成后zip文件的地址以及zip文件名");
        Scanner sc=new Scanner(System.in);
        String target=sc.next();
        String name=sc.next();
        System.out.println("请输入需要压缩的文件地址及名字,以#为结束标志");
        String file=sc.nextLine();
        ListfileList=new ArrayList<>();
        while(!file.equals("#")){
            fileList.add(new File(file));
            file=sc.nextLine();
        }
        try{
            String pa=target+"\\"+name+".zip";
            FileOutputStream fos=new FileOutputStream(new File(pa));
            toZip(fileList,fos);
            System.out.println("压缩成功!!!");
        }catch (IOException e){
            System.out.println("压缩失败....");
            e.printStackTrace();
        }*/
    }
    public static void toZip(List srcFiles , OutputStream out)throws RuntimeException {
        ZipOutputStream zos = null ;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[1024];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1){
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipCreator",e);
        }finally{
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void pressedFile(String path,String targetPath,String name)throws Exception{
        File reFile=new File(path);
        File tarFile=new File(targetPath);
        if(!tarFile.exists()){
            tarFile.mkdirs();
        }
        String targetName=name+".zip";
        FileOutputStream os = new FileOutputStream(tarFile + "\\" + targetName);
        ZipOutputStream out = new ZipOutputStream((new BufferedOutputStream(os)));
        creatZip(out,reFile,"");
    }
    public static void creatZip(ZipOutputStream out,File file,String dir)throws Exception{
        if(file.isDirectory()){
            File []files=file.listFiles();
            out.putNextEntry(new ZipEntry(dir+"/"));
            dir=dir.length()==0?"":dir+"/";
            for(int i = 0 ; i < files.length ; i++){
                creatZip(out,files[i],dir + files[i].getName());
            }
        }
        else{
            FileInputStream fis=new FileInputStream(file);
            out.putNextEntry(new ZipEntry(dir));
            int j=0;
            byte[] buffer = new byte[1024];
            while((j=fis.read(buffer))>0){
                out.write(buffer,0,j);
        }
            fis.close();
}


    }
}

用户输入不同文件夹的文件,也许还包括其他电脑中的文件,然后程序将这些文件打包成zip文件。复杂一点的话,打包时对这些文件进行压缩。

你可能感兴趣的:(java)