File工具

FIle工具

读取ZIP压缩包

public class FileUtils{
	public static long getSize(InputStream is){
		if(is == null){
			return 0l;
		}
		if(is.markSupported()){
			return getSizeMark(is);
		}
		return getSizeMark;
	}
	
	private static long getSizeNoMark(InputStream is){
	    // 缓冲区大小1k
	    byte[] buff = new byte[1024];
	    BufferedInputStream bis = null;
	    long size = 0l;
	    try {
	        bis = new BufferedInputStream(is);
	        int read = bis.read(buff);
	        size += read;
	        // 通过while循环写入到指定了的文本流中
	        while(read != -1){
	            read = bis.read(buff);
	            size += read;
	        }
	        size++;// 抵最后一次的-1
	    } catch(IOException e) {
	        e.printStackTrace();
	    }
	    return size;
	}
	
	private static long getSizeMark(InputStream is){
	    // 缓冲区大小1k
	    byte[] buff = new byte[1024];
	    BufferedInputStream bis = null;
	    long size = 0l;
	    try {
	        is.mark(Integer.MAX_VALUE);
	        bis = new BufferedInputStream(is);
	        int read = bis.read(buff);
	        size += read;
	        // 通过while循环写入到指定了的文本流中
	        while(read != -1){
	            read = bis.read(buff);
	            size += read;
	        }
	        size++;// 抵最后一次的-1
	        is.reset();
	    } catch(IOException e) {
	        e.printStackTrace();
	    }
	    return size;
	}
	
	public static void writeTextToFile(String text,File file,boolean append){
	    FileOutputStream fos = null;
	    OutputStreamWriter osw = null;
	    BufferedWriter bw = null;
	    try {
	        if(!file.exists()){
	            file.createNewFile();
	        }
	        fos = new FileOutputStream(file,append);
	        osw = new OutputStreamWriter(fos,"UTF-8");
	        bw = new BufferedWriter(osw);
	        
	        bw.write(text.trim());
	    } catch(IOException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            if(bw != null){
	                bw.flush();
	                bw.close();
	            }
	            if(osw != null){
	                osw.close();
	            }
	            if(fos != null){
	                fos.close();
	            }
	        } catch(IOException e) {
	            e.printStackTrace();
	        }
	    }
	    
	    public static boolean compress(File zip,File[] sources,String dir){
	        ZipOutputStream zos = null;
	        FileOutputStream fos = null;
	        try{
	            if(!zip.getParentFile().exists()){
	                zip.getParentFile().mkdirs();
	            }
	            if (!zip.exists()){
	                zip.createNewFile();
	            } 
	            fos = new FileOutputStream(zip);
	            zos = new ZipOutputStream(zip);
	            zip(sources,dir,zos);
	        } catch (IOException e) {// 文件压缩异常
	            e.printStackTrace();
	        } finally {
	            try {
	                if (zos != null){
	                    zos.close();
	                } 
	                if (fos != null){
	                    fos.close();
	                } 
	            } catch (IOException e){// 文件压缩异常-关流
	                e.printStackTrace();
	            }
	        }
	    }
	    return true;
	}
	
	private static void zip(File[] sources,String dir,ZipOutputStream zos){
	    for (File source : sources){
	        if (source.isDirectory()){
	            compressDir(source,dir,zos);
	        } else {
	            doZip(zos,source,dir);
	        }
	    } 
	}
	
	private static void compressDir(File source,String dir,ZipOutputStream zos){
	    File[] files = source.listFiles();
	    if (files != null && files.length > 0){
	        zip(files,dir + File.separator + source.getName(),zos);
	    } 
	}
	
	private static void doZip(ZipOutputStream zos,File source,String dir){
	    String entryName = null;
	    if (dir != null && dir != ""){
	        entryName = dir + "/" + source.getName();
	    } else {
	        entryName = source.getName();
	    }
	    ZipEntry entry = new ZipEntry(entryName);
	    zos.putNextEntry(entry);
	    
	    FileInputStream fis = null;
	    try {
	        int len = 0;
	        byte[] buffer = new byte[1024];
	        fis = new FileInputStream(source);
	        while((len = fis.read(buffer)) > 0){
	            zos.write(buffer,0,len);
	            zos.flush();
	        }
	    } finally {
	        zos.closeEntry();
	        if (fis != null)
	            fis.close();
	    }
	}
	
	public static void main(String[] args){
	    String zipPath = "D:\\myzip\\test.zip";
	    String path = "D:\\myzip";
	    String dir = "";
	    try {
	        compress(new File(zipPath),(new File(path)).listFiles(),dir);
	    } catch(Exception e) {
	        e.printStackTrace();
	    }
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值