Administrator
2021-01-19 fbed4c7738db69c0d6a30c10f6944268ba28ea9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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");
    }
}