0

我见过使用 java.util.zip.ZipEntry 我们可以压缩文件。

我可以压缩它,而且我正在将它从一个FTP位置传输到另一个 FTP 位置

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

我还看到有一些方法 FTP.sendCommand() 。但不确定如何使用它发送命令以将文件压缩到一个 FTP 位置并使用此方法复制到另一个 lcoation。

有人对此有任何想法吗?

4

1 回答 1

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. 完成后,关闭所有流

于 2012-10-11T18:42:53.073 回答