JAVAはftpでファイルをリモート取得し圧縮する


①ftpによるファイルのリモート取得
public boolean getftp(String host, String login, String pass,
String remoteFile, String localFile, String filename)
throws Exception {
boolean resultFlg = false;
FtpClient ftp = new FtpClient();
try {
ftp.openServer(host);
try {
ftp.login(login, pass);
ftp.binary();
OutputStream out = new FileOutputStream(localFile);
TelnetInputStream ftpIn = ftp.get(remoteFile);
byte[] buff = new byte[204800];
int len = 0;
try {
while ((len = ftpIn.read(buff)) != -1) {
out.write(buff, 0, len);
resultFlg = true;
}
} catch (IOException ioe) {
resultFlg = false;
} finally {
if (ftpIn != null) {
ftpIn.close();
}
if (out != null) {
out.close();
}
}
} catch (Exception ex) {
resultFlg = false;
} finally {
ftp.closeServer();
}
} catch (Exception e) {
}
return resultFlg;
}
注意:ファイル名に【中国語名】が含まれていると文字化けしてしまう可能性があります.shellに符号化フォーマットunixシステムを設定して符号化フォーマットコマンド(locale)を表示するには
unix(LANG) SJIS: ja_JP.PCK; EUC:ja; UTF8:ja_JP.UTF-8 #!/bin/csh
setenv LANG ja_JP.PCK

private void writeZip(File file, String parentPath, ZipOutputStream zos) {
if (file.exists()) {
if (file.isDirectory()) {
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
writeZip(files[i], parentPath, zos);
}
} else {
FileInputStream fis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
dis = new DataInputStream(new BufferedInputStream(fis));
String zipName = parentPath + file.getName();
ZipEntry ze = new ZipEntry(new String(zipName.getBytes(),"Shift_JIS"));
zos.putNextEntry(ze);
byte[] content = new byte[1024];
int len;
while ((len = fis.read(content)) != -1) {
zos.write(content, 0, len);
zos.flush();
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
try {
if (dis != null) {
dis.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
}
}
}
}
}
: , apache ant.jar
Javaが した キットimport java.util.zip.ZipEntryを しないでください.import java.util.zip.ZipOutputStream; けしますから.
③フォルダおよびフォルダ のファイルの
private boolean deleteDir(File dir) {if (dir.isDirectory()) {             String[] children = dir.list();             for (int i=0; i                 boolean success = deleteDir(new File(dir, children[i]));                 if (!success) {                     return false;                 }             }         }         return dir.delete(); }
これで わりです