2012-10-11 19 views
0

我見過使用java.util.zip.ZipEntry我們可以壓縮文件。java ftp zip更好的方法

我能壓縮它,還我從一個FTP位置和其他FTP位置

outStream.putNextEntry(new ZipEntry()); 
while ((ByteRead = is.read(buf)) != -1) 
{  
    outStream.write(buf, 0, ByteRead); 
    ByteWritten += ByteRead; 
} 

我也看到它轉移有沒有一些方法FTP.sendCommand()。 但不知道如何使用它發送命令來壓縮一個FTP位置的文件並使用此方法複製到另一個文件。

有沒有人對此有任何意見?

+0

FTP.sendCommand()用於FTP命令,爲了壓縮遠程文件,我會說你需要使用SSH。 – lynks

+0

[zip文件存在於一個FTP位置並直接複製到另一個FTP位置]的可能重複(http://stackoverflow.com/questions/12933180/zip-the-files-which-are-present-at- one-ftp-location-and-copy-to-another-ftp-loca) – fglez

回答

1

我想你實現這兩個步驟:

  1. 郵編文件,並在第一個FTP位置

    URL ftpLocation1 = new URL("ftp://url1"); 
    URLConnection ftpConnect1 = ftpLocation1.openConnection(); 
    OutputStream ftpOutStream1 = ftpConnect1.getOutputStream(); // To upload 
    ftpOutStream1.putNextEntry(new ZipEntry()); 
    while ((ByteRead = is.read(buf)) != -1) {  
        ftpOutStream1.write(buf, 0, ByteRead); 
        ByteWritten += ByteRead; 
    } 
    
  2. 寫讀zip文件,因爲它是和寫入到第二FTP位置

    InputStream ftpInputStream1 = ftpConnect1.getInputStream(); // To read back 
    URL ftpLocation2 = new URL("ftp://url2"); 
    URLConnection ftpConnect2 = ftpLocation1.openConnection(); 
    OutputStream ftpOutStream2 = ftpConnect2.getOutputStream(); // To upload 
    
    //read through ftpInputStream1 and write in ftpOutStream2 
    while ((ByteRead = ftpInputStream1.read(buf)) != -1) {  
        ftpOutStream2.write(buf, 0, ByteRead); 
        ByteWritten += ByteRead; 
    } 
    
  3. 完成後,關閉所有的流

+0

謝謝大家......我嘗試了各種各樣的東西...... – user1739064