import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Eddy {
public static void main(String[] args)
{
try{
File f = new File("C:/onlinelog.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis); //缓冲输入流(BufferedInputStream)
byte[] buf = new byte[1024];
int len; //set len variable
FileOutputStream fos = new FileOutputStream(f.getName()+".zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);//压缩包 //zip输出流(stream)
ZipEntry ze = new ZipEntry(f.getName());//这是压缩包名里的文件名//zipEntry类
ze.setTime(System.currentTimeMillis());
System.out.println(ze.toString());
System.out.println(ze.getName());
System.out.println(ze.clone()); //clone
zos.putNextEntry(ze);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处
while((len=bis.read(buf))!=-1) //当!=-1 不等于-1时。也就是有数据。执行以下语句块
{
zos.write(buf,0,len); //第一个参数 byte数组buf,第二个offset,第三个len
zos.flush();
System.out.println(ze.getSize()); //得到onlinelog大小,未知返回-1
}
try{
bis.close();
zos.close();
}
catch(Exception e){
e.printStackTrace();
System.err.println("bis.close() zos.close() error");
}
}
catch(Exception ex){
ex.printStackTrace();
System.err.println("error");
}
}
}