-1

I am downloading a file from a FTP server using the ftp crate:

extern crate ftp;
extern crate chrono;

use std::env;
use std::fs::File;
use std::str;
use std::io::{Result, Write};
use ftp::FtpStream;
use chrono::*;


fn main() {

    let args: Vec<String> = env::args().collect();
    ftpgp(&args[1], &args[2], &args[3], &args[4]).unwrap_or_else(|err|
        panic!("{}", err)
    );

}

fn ftpgp(addr: &str, user: &str, pass: &str, remote_path: &str,) -> Result<()> {
    let dt = Local::now();
    let now = dt.format("%Y-%m-%d %H:%M:%S").to_string();

    let mut ftp_stream = try!(FtpStream::connect((addr, 21)));
    try!(ftp_stream.login(user, pass));

    ftp_stream.cwd(remote_path).unwrap();
    let file_list = try!(ftp_stream.nlst(None));

    let files: Vec<String> = file_list.into_iter()
        .filter(|name| name.ends_with(".zip"))
        .collect();

    for file in files {
        try!(ftp_stream.retr(file.as_str(), |stream| {
            let mut file = File::create(file.clone()).unwrap();
            let mut buf = [0; 2048];

            loop {
              match stream.read(&mut buf) {
                Ok(0) => break,
                Ok(n) => file.write_all(&buf[0..n]).unwrap(),
                Err(err) => return Err(err)
              };
            }

            Ok(())
        }));
        println!("{} Saving: {}", now, file);

        ftp_stream.rm(file.as_str()).unwrap();
        println!("{} Delete from ftp: {}", now, file);
    }

    ftp_stream.quit()
}

But zip files are corrupt after downloading:

   unzip -t sample.zip 
   Archive:  sample.zip
   warning [sample.zip]:  252314 extra bytes at beginning or within zipfile
      (attempting to process anyway)
   file #1:  bad zipfile offset (local header sig):  252314
      (attempting to re-compensate)
   testing: sample.pdf  
      error:  invalid compressed data to inflate
   file #2:  bad zipfile offset (local header sig):  67841095
      (attempting to re-compensate)
   file #2:  bad zipfile offset (local header sig):  67841095
   file #3:  bad zipfile offset (local header sig):  68093906

I have set the type of file to be transferred, but I don't know how.

Like this:

ftp_stream.transfer_type(Binary);

is not working.

error: unresolved name `Binary`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Anton
  • 3
  • 2

1 Answers1

0

This is just a name resolution issue.

You have declared a dependency on an external crate:

extern crate ftp;

And then imported one symbol from this crate:

use ftp::FtpStream;

Therefore, the symbol ::ftp::types::FileType::Binary is not in scope.

You can either use the full name, or modify your import:

  • use ftp::{FtpStream, types} means you can write types::FileType::Binary
  • use ftp::types::FileType means you can write FileType::Binary
  • use ftp::types::FileType::Binary means you can write Binary

It's really a matter of taste.

Also note that if you wish you can put the use statement in the only function using Binary to tighten the scope of the import; it's something I like to make it clearer where the imports are used (and where they are not).

Anton
  • 3
  • 2
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722