package com.backup;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.util.Date;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipOutputStream;
|
|
import com.base.Com;
|
import com.config.AppConfig;
|
|
public class HzipSocket{
|
//public static String targetip = "118.89.139.230"; //Ä¿±êipµØÖ·
|
//public static String targetip = "192.168.7.123"; //Ä¿±êipµØÖ·
|
public static int targetport = 10100; //Ä¿±ê¶Ë¿ÚºÅ
|
private AppConfig cfg;
|
|
public HzipSocket(AppConfig cfg) {
|
this.cfg = cfg;
|
//System.err.println(cfg.getSourceSQLServerIp()+"########");
|
}
|
|
public void init(String filepath) {
|
Socket s = null;
|
try {
|
s = new Socket(cfg.getSourceSQLServerIp(),10100);
|
//Òª·¢ËͳöÈ¥µÄÎļþ¼Ð.Çë×¢ÒâÔÚ²»Í¬²Ù×÷ϵͳÉÏ,±íʾ·½·¨¿ÉÄܲ»Ò»Ñù¡£
|
File file =new File(filepath);
|
OutputStream os=s.getOutputStream();
|
ZipOutputStream gout=new ZipOutputStream(os);
|
byte[] b=new byte[1024];
|
zipEntry(file, gout,b);
|
} catch (IOException e) {
|
e.printStackTrace();
|
System.out.println(cfg.getSourceSQLServerIp()+"Ô¶³Ì±¸·ÝÊý¾Ý¿âʧ°Ü"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
} finally {
|
if(null != s) {
|
try {
|
s.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
//ÎÞÂÛÎļþ£¬»¹ÊÇÎļþ¼Ð¶¼ÊÇÒ»¸öZipEntry¡£
|
/**
|
* ÕâÀïĬÈÏËùÓÐÎļþ¶¼¿ÉÒÔ¶Áд£¬µ«ÔÚʵ¼ÊÓ¦ÓÃÖУ¬
|
* ΪÁ˳ÌÐòµÄ½¡×³ÐÔ£¬±ØÐëÅжÏÎļþÊÇ·ñ¿É¶Á¡¢¿Éд£¬
|
* ÊÇ·ñÊÇÒþ²ØÎļþµÈ£¬ÔÚ°²×¿ÀïÕâÖÖÇéÐκܳ£¼û¡£
|
* ÀýÈ磬²»¿É¶ÁµÄÎļþ£¬×ÔȻҲ¾Íû·¨´«ÊäÁË¡£
|
* @param file Òª´«Ë͵ÄÎļþ¼Ð¡£
|
* @param zos °ü×°ºóµÄsocketµÄoutputstream
|
* @param b »º´æËùÓá£Ò»¸öÈÝÆ÷¡£
|
*/
|
private void zipEntry(File file,ZipOutputStream zos,byte[] b)
|
{
|
if(file.isDirectory())//Èç¹ûÎļþÊÇĿ¼£¬¾ÍûʲôÐèÒª´«ÊäµÄÁË£¬
|
//Ö»ÒªÔÚzosÀï·Å½øÒ»¸öZipEntry,È»ºóµÝ¹éËüÀïÃæµÄÎļþ
|
{
|
//ZipEntryÓиö·½·¨isDirectory(),¿ÉÅжϸÃZipEntry±íʾµÄ
|
//¶øÊDz»ÊÇĿ¼£¨Îļþ¼Ð£©£¬ÒÀ¾Ý¾ÍÊǸÃZipEntryµÄnameÊDz»ÊÇÒÔ¡°/¡±½áβµÄ¡£
|
ZipEntry ze=new ZipEntry(file.getAbsolutePath()+"/");
|
|
try {
|
zos.putNextEntry(ze);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
File[] files=file.listFiles();
|
for(File f:files)
|
{
|
zipEntry(f, zos,b);
|
}
|
}
|
else
|
{
|
ZipEntry ze=new ZipEntry(file.getAbsolutePath());
|
try {
|
zos.putNextEntry(ze);
|
FileInputStream ins = new FileInputStream(file);
|
int length=-1;
|
while( (length=ins.read(b))>-1 )
|
{
|
zos.write(b, 0, length);
|
zos.flush();
|
}
|
zos.flush();
|
zos.closeEntry();
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
AppConfig cfg = new AppConfig();
|
HzipSocket h = new HzipSocket(cfg);
|
h.init("d:/temp");
|
}
|
}
|
|