1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.crosswire.jsword.book.install.sword; |
21 | |
|
22 | |
import java.io.IOException; |
23 | |
|
24 | |
import org.apache.commons.net.ftp.FTPClient; |
25 | |
import org.apache.commons.net.ftp.FTPFile; |
26 | |
import org.apache.commons.net.ftp.FTPReply; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public final class FTPExample { |
35 | 0 | private FTPExample() { } |
36 | |
|
37 | |
static void copyDirectory(FTPClient ftpClient, String sourceDir, String module, String destDir, String currentDir) throws IOException { |
38 | 0 | String dirToList = module; |
39 | 0 | if (!"".equals(currentDir)) { |
40 | 0 | dirToList += "/" + currentDir; |
41 | |
} |
42 | 0 | FTPFile[] subFiles = ftpClient.listFiles(sourceDir + '/' + dirToList); |
43 | 0 | if (subFiles != null && subFiles.length > 0) { |
44 | 0 | for (FTPFile aFile : subFiles) { |
45 | 0 | String currentFileName = aFile.getName(); |
46 | |
|
47 | 0 | if (".".equals(currentFileName) || "..".equals(currentFileName)) { |
48 | 0 | continue; |
49 | |
} |
50 | 0 | if (aFile.isFile()) { |
51 | 0 | copyFile(ftpClient, sourceDir, dirToList + '/' + currentFileName, destDir); |
52 | 0 | } else if (aFile.isDirectory() && !"lucene".equalsIgnoreCase(currentFileName)) { |
53 | 0 | System.out.println("mkdir " + destDir + dirToList + '/' + currentFileName + "/"); |
54 | 0 | copyDirectory(ftpClient, sourceDir, dirToList, destDir, currentFileName); |
55 | |
} |
56 | |
} |
57 | |
} |
58 | 0 | } |
59 | |
|
60 | |
static void copyFile(FTPClient ftpClient, String sourceDir, String file, String destDir) throws IOException { |
61 | 0 | System.out.println("cp " + sourceDir + '/' + file + ' ' + destDir + '/' + file); |
62 | 0 | } |
63 | |
|
64 | |
static long getSize(FTPClient ftpClient, String sourceDir, String module) throws IOException { |
65 | 0 | long total = 0; |
66 | 0 | String dirToList = module; |
67 | 0 | FTPFile[] subFiles = ftpClient.listFiles(sourceDir + '/' + dirToList); |
68 | 0 | if (subFiles != null && subFiles.length > 0) { |
69 | 0 | for (FTPFile aFile : subFiles) { |
70 | 0 | String currentFileName = aFile.getName(); |
71 | 0 | if (".".equals(currentFileName) || "..".equals(currentFileName)) { |
72 | |
|
73 | 0 | continue; |
74 | |
} |
75 | 0 | if (aFile.isFile()) { |
76 | 0 | total += aFile.getSize(); |
77 | 0 | } else if (aFile.isDirectory() && !"lucene".equalsIgnoreCase(currentFileName)) { |
78 | 0 | total += getSize(ftpClient, sourceDir, dirToList); |
79 | |
} |
80 | |
} |
81 | |
} |
82 | 0 | return total; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public static void main(String[] args) { |
100 | 0 | String server = "ftp.crosswire.org"; |
101 | 0 | int port = 21; |
102 | 0 | String user = "anonymous"; |
103 | 0 | String pass = "jsword@crosswire.org"; |
104 | 0 | String dirToList = "/pub/sword/avraw"; |
105 | 0 | String confPath = "mods.d/azeri.conf"; |
106 | 0 | String dataPath = "modules/texts/ztext/azeri"; |
107 | |
|
108 | 0 | FTPClient ftpClient = new FTPClient(); |
109 | |
try { |
110 | 0 | ftpClient.connect(server, port); |
111 | 0 | int replyCode = ftpClient.getReplyCode(); |
112 | 0 | if (!FTPReply.isPositiveCompletion(replyCode)) { |
113 | 0 | System.out.println("Connect failed"); |
114 | |
return; |
115 | |
} |
116 | 0 | boolean success = ftpClient.login(user, pass); |
117 | 0 | if (!success) { |
118 | 0 | System.out.println("Could not login to the server"); |
119 | |
return; |
120 | |
} |
121 | 0 | ftpClient.setUseEPSVwithIPv4(true); |
122 | 0 | copyFile(ftpClient, dirToList, confPath, "/Users/DM/Library/Application Support/Sword"); |
123 | 0 | System.out.println("Size is " + getSize(ftpClient, dirToList, confPath)); |
124 | 0 | copyDirectory(ftpClient, dirToList, dataPath, "/Users/DM/Library/Application Support/Sword", ""); |
125 | 0 | System.out.println("Size is " + getSize(ftpClient, dirToList, dataPath)); |
126 | 0 | } catch (IOException e) { |
127 | 0 | System.out.println("Oops! Something wrong happened"); |
128 | 0 | e.printStackTrace(); |
129 | |
} finally { |
130 | |
|
131 | 0 | try { |
132 | 0 | if (ftpClient.isConnected()) { |
133 | 0 | ftpClient.logout(); |
134 | 0 | ftpClient.disconnect(); |
135 | |
} |
136 | 0 | } catch (IOException ex) { |
137 | 0 | System.out.println("Oops! Something wrong happened"); |
138 | 0 | ex.printStackTrace(); |
139 | 0 | } |
140 | 0 | } |
141 | 0 | } |
142 | |
} |