RAR

闇�瑕乤nt.jar鍖�

閾炬帴: https://pan.baidu.com/s/1uLQdjit3gjgBpWaVRRXNoA 鎻愬彇鐮�: ydn7聽


import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.zip.CRC32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipFile;

public class Main {

public static void main(String[] args)throws IOException {

System.out.println("Hello World!");

File one =new File("//home//weikun//鏂囨。//1996.tar.gz");

Date now=new Date();

SimpleDateFormat time=new SimpleDateFormat("YYY-MM-DD");

System.out.println(time.format(now));

File two=new File(time.format(now)+".rar");

FileOutputStream fos=new FileOutputStream(two);

CheckedOutputStream cos=new CheckedOutputStream(fos,new CRC32());

ZipOutputStream zos=new ZipOutputStream(cos);

zos.setEncoding("GBK");

ZipFile(zos,one);

zos.close();

cos.close();

fos.close();

System.out.println("鍘嬬缉瀹屾垚");

System.out.println(two.getAbsolutePath());

}

public static void ZipFile(ZipOutputStream zos,File file)throws IOException {

if(file.isDirectory()){

//鍘嬬缉鏂囦欢鐨勭洰褰曠粨鏋�

聽 聽 聽 聽 聽 聽 zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(file.getName()))+File.separator));

for (File f:file.listFiles()){

ZipFile(zos,f);

}

}

//鎵撳嵃杈撳嚭姝e湪鍘嬬缉鐨勬枃浠�

聽 聽 聽 聽 System.out.println("姝e湪鍘嬬缉鏂囦欢:"+file.getName());

//鍒涘缓鍘嬬缉鏂囦欢

聽 聽 聽 聽 zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(file.getName()))));

//鐢ㄥ瓧鑺傛柟寮忚鍙栨簮鏂囦欢

聽 聽 聽 聽 InputStream is =new FileInputStream(file.getPath());

//鍒涘缓涓�涓紦瀛樺尯

聽 聽 聽 聽 BufferedInputStream bis =new BufferedInputStream(is);

//瀛楄妭鏁扮粍,姣忔璇诲彇1024涓瓧鑺�

聽 聽 聽 聽 byte [] b =new byte[1024];

//寰幆璇诲彇锛岃竟璇昏竟鍐�

聽 聽 聽 聽 while(bis.read(b)!=-1)

{

zos.write(b);//鍐欏叆鍘嬬缉鏂囦欢

聽 聽 聽 聽 }

//鍏抽棴娴�

聽 聽 聽 聽 bis.close();

is.close();

}

}

你可能感兴趣的:(RAR)