ftpconnect 0.1.6

  • Readme
  • Changelog
  • Example
  • Installing
  • new75

Flutter FTP Connect

Flutter simple and robust dart FTP Connect Library to interact with FTP Servers with possibility of zip and unzip files.

Build

Key FeaturesExamplesLicense

Key Features #

  • Upload files to FTP
  • Download files/directories from FTP
  • List FTP directory contents
  • Manage FTP files (rename/delete)
  • Manage file zipping/unzipping
  • Completely asynchronous functions

Example upload file #

###example 1:

import 'dart:io';
import 'package:ftpconnect/ftpConnect.dart';

main() async{
    FTPConnect ftpConnect = FTPConnect('example.com',user:'user', pass:'pass');
    File fileToUpload = File('fileToUpload.txt');
    bool res = await ftpConnect.uploadFileWithRetry(fileToUpload, pRetryCount: 2);
    print(res);
}

###example 2: step by step

import 'dart:io';
import 'package:ftpconnect/ftpConnect.dart';

main() async{
  FTPConnect ftpConnect = FTPConnect('example.com',user:'user', pass:'pass');
 try {
      File fileToUpload = File('fileToUpload.txt');
      await ftpConnect.connect();
      await ftpConnect.uploadFile(fileToUpload);
      await ftpConnect.disconnect();
    } catch (e) {
      //error
    }
}

Download file #

###example 1:

import 'dart:io';
import 'package:ftpconnect/ftpConnect.dart';

main() async{
    FTPConnect ftpConnect = FTPConnect('example.com',user:'user', pass:'pass');
    String fileName = 'toDownload.txt';
    bool res = await ftpConnect.downloadFileWithRetry(fileName, File('myFileFromFTP.txt'));
    print(res)
}

###example 2: step by step

import 'dart:io';
import 'package:ftpconnect/ftpConnect.dart';

main() {
  FTPConnect ftpConnect = FTPConnect('example.com',user:'user', pass:'pass');
 try {
      String fileName = 'toDownload.txt';
      await ftpConnect.connect();
      await ftpConnect.downloadFile(fileName, File('myFileFromFTP.txt'));
      await ftpConnect.disconnect();
    } catch (e) {
      //error
    }
}

Other Features #

###Directory functions:

//Get directory content
ftpConnect.listDirectoryContent();

//Create directory
ftpConnect.makeDirectory('newDir');

//Change directory
ftpConnect.changeDirectory('moveHereDir');

//get current directory
ftpConnect.currentDirectory();

//Delete directory
ftpConnect.deleteDirectory('dirToDelete');

//check for directory existance
ftpConnect.checkFolderExistence('dirToCheck');

//create a directory if it does not exist
ftpConnect.createFolderIfNotExist('dirToCreate');

###File functions:

//rename file
ftpConnect.rename('test1.txt', 'test2.txt');

//file size
ftpConnect.sizeFile('test1.txt');

//file existence
ftpConnect.existFile('test1.txt');

//delete file
ftpConnect.deleteFile('test2.zip');

###Zip functions:

//compress a list of files/directories into Zip file
FTPConnect.zipFiles(List<String> paths, String destinationZipFile);

//unzip a zip file
FTPConnect.unZipFile(File zipFile, String destinationPath, {password});

Paramaters #

PropertiesDescription
hostHostname or IP Address
portPort number (Defaults to 21)
userUsername (Defaults to anonymous)
passPassword if not anonymous login
debugEnable Debug Logging
timeoutTimeout in seconds to wait for responses (Defaults to 30)

View more Examples #

License #

MIT

[0.1.6] - 2020.05.22 #

  • throw exception if directory doesn't exist

[0.1.5] - 2020.05.21 #

  • improve directory download

[0.1.4] - 2020.05.19 #

  • make all functions async

[0.1.3] - 2020.05.18 #

  • improve Directory download

[0.1.2] - 2020.05.18 #

  • add test dependency

[0.1.1] - 2020.05.18 #

  • Add download a folder feature

[0.1.0] - 2020.05.08 #

  • Update description

[0.0.1] - 2020.05.07 #

  • First publication

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:ftpconnect/ftpconnect.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter FTP Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final ValueNotifier<String> _logNotifier = ValueNotifier('');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Example FTP")),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          ButtonBar(
            alignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: _uploadStepByStep,
                child: Text("Upload step by step"),
                color: Theme.of(context).primaryColorDark,
              ),
              RaisedButton(
                onPressed: _uploadWithRetry,
                child: Text("Upload with retry"),
                color: Theme.of(context).primaryColorDark,
              )
            ],
          ),
          ButtonBar(
            alignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: _downloadStepByStep,
                child: Text("Download step by step"),
                color: Theme.of(context).primaryColor,
              ),
              RaisedButton(
                onPressed: _downloadWithRetry,
                child: Text("Download with retry"),
                color: Theme.of(context).primaryColor,
              )
            ],
          ),
          ButtonBar(
            alignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: _uploadWithCompress,
                child: Text("Compress & Upload Zip"),
                color: Theme.of(context).primaryColorLight,
              ),
              RaisedButton(
                onPressed: _downloadZipAndUnZip,
                child: Text("Download Zip & decompress"),
                color: Theme.of(context).primaryColorLight,
              )
            ],
          ),
          RaisedButton(
            child: Text("Download Directory"),
            onPressed: () => _downloadDirectory(),
          ),
          ValueListenableBuilder(
              valueListenable: _logNotifier,
              builder: (context, String text, widget) {
                return Text(text ?? '');
              })
        ],
      ),
    );
  }

  Future<void> _uploadStepByStep() async {
    FTPConnect ftpConnect =
        FTPConnect("example.net", user: 'user', pass: 'pass');

    try {
      await _log('Connecting to FTP ...');
      await ftpConnect.connect();
      File fileToUpload = await _fileMock(
          fileName: 'uploadStepByStep.txt', content: 'uploaded Step By Step');
      await _log('Uploading ...');
      await ftpConnect.uploadFile(fileToUpload);
      await _log('file uploaded sucessfully');
      await ftpConnect.disconnect();
    } catch (e) {
      await _log('Error: ${e.toString()}');
    }
  }

  Future<void> _uploadWithRetry() async {
    try {
      FTPConnect ftpConnect =
          FTPConnect("example.net", user: 'user', pass: 'pass');

      File fileToUpload = await _fileMock(
          fileName: 'uploadwithRetry.txt', content: 'uploaded with Retry');
      await _log('Uploading ...');
      bool res =
          await ftpConnect.uploadFileWithRetry(fileToUpload, pRetryCount: 2);
      await _log('file uploaded: ' + (res ? 'SUCCESSFULLY' : 'FAILED'));
    } catch (e) {
      await _log('Downloading FAILED: ${e.toString()}');
    }
  }

  Future<void> _downloadWithRetry() async {
    try {
      await _log('Downloading ...');
      FTPConnect ftpConnect =
          FTPConnect("example.net", user: 'user', pass: 'pass');

      String fileName = 'flutter/test.txt';
      //here we just prepare a file as a path for the downloaded file
      File downloadedFile = await _fileMock(fileName: 'downloadwithRetry.txt');
      bool res = await ftpConnect
          .downloadFileWithRetry(fileName, downloadedFile, pRetryCount: 2);
      await _log('file downloaded  ' +
          (res ? 'path: ${downloadedFile.path}' : 'FAILED'));
    } catch (e) {
      await _log('Downloading FAILED: ${e.toString()}');
    }
  }

  Future<void> _downloadStepByStep() async {
    try {
      await _log('Connecting to FTP ...');
      FTPConnect ftpConnect =
          FTPConnect("example.net", user: 'user', pass: 'pass');

      await ftpConnect.connect();

      await _log('Downloading ...');
      String fileName = 'flutter/test.txt';

      //here we just prepare a file as a path for the downloaded file
      File downloadedFile = await _fileMock(fileName: 'downloadStepByStep.txt');
      await ftpConnect.downloadFile(fileName, downloadedFile);
      await _log('file downloaded path: ${downloadedFile.path}');
      await ftpConnect.disconnect();
    } catch (e) {
      await _log('Downloading FAILED: ${e.toString()}');
    }
  }

  Future<void> _uploadWithCompress({String filename = 'flutterZip.zip'}) async {
    try {
      FTPConnect ftpConnect =
          FTPConnect("example.net", user: 'user', pass: 'pass');

      await _log('Compressing file ...');

      File fileToCompress = await _fileMock(
          fileName: 'fileToCompress.txt', content: 'uploaded into a zip file');
      final zipPath = (await getTemporaryDirectory()).path + '/$filename';

      await FTPConnect.zipFiles([fileToCompress.path], zipPath);

      await _log('Uploading Zip file ...');
      bool res =
          await ftpConnect.uploadFileWithRetry(File(zipPath), pRetryCount: 2);
      await _log('Zip file uploaded: ' + (res ? 'SUCCESSFULLY' : 'FAILED'));
    } catch (e) {
      await _log('Upload FAILED: ${e.toString()}');
    }
  }

  Future<void> _downloadZipAndUnZip() async {
    try {
      //this will upload a flutterZip.zip file (create a ftp file to be downloaded)
      String ftpFileName = 'flutterZip.zip';
      await _uploadWithCompress(filename: ftpFileName);
      //we delete the file locally
      File((await getTemporaryDirectory()).path + '/$ftpFileName').deleteSync();
      //start downloading the zip file
      await _log('Downloading Zip file...');

      FTPConnect ftpConnect =
          FTPConnect("example.net", user: 'user', pass: 'pass');

      //here we just prepare a file as a path for the downloaded file
      File downloadedZipFile = await _fileMock(fileName: 'ZipDownloaded.zip');
      bool res = await ftpConnect.downloadFileWithRetry(
          ftpFileName, downloadedZipFile);
      if (res) {
        await _log('Zip file downloaded  path: ${downloadedZipFile.path}');
        await _log('UnZip files...');
        await _log('origin zip file\n' +
            downloadedZipFile.path +
            '\n\n\n Extracted files\n' +
            (await FTPConnect.unZipFile(
                    downloadedZipFile, downloadedZipFile.parent.path))
                .reduce((v, e) => v + '\n' + e));
      } else {
        await _log('Zip file downloaded FAILED');
      }
    } catch (e) {
      await _log('Downloading FAILED: ${e.toString()}');
    }
  }

  Future<void> _downloadDirectory() async {
    try {
      FTPConnect ftpConnect =
          FTPConnect("example.net", user: 'user', pass: 'pass');

      await _log('Download Directory  ...');

      var localDir =
          Directory((await getExternalStorageDirectory()).path + '/flutter')
            ..createSync(recursive: true);
      var res = await ftpConnect.downloadDirectory(
          'entrant/ND2/TabErwan/DashBoard', localDir);

      await _log('Downloading directory: ' + (res ? 'SUCCESSFULLY' : 'FAILED'));
    } catch (e) {
      await _log('Downloading directory FAILED: ${e.toString()}');
    }
  }

  ///an auxiliary function that manage showed log to UI
  Future<void> _log(String log) async {
    _logNotifier.value = log;
    await Future.delayed(Duration(seconds: 1));
  }

  ///mock a file for the demonstration example
  Future<File> _fileMock({fileName = 'FlutterTest.txt', content = ''}) async {
    final Directory directory =
        Directory((await getExternalStorageDirectory()).path + '/test')
          ..createSync(recursive: true);
    final File file = File('${directory.path}/$fileName');
    await file.writeAsString(content);
    return file;
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ftpconnect: ^0.1.6

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ftpconnect/ftpconnect.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on May 30, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev
  • Flutter: 1.17.1

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
archive ^2.0.13 2.0.13
flutter 0.0.0
path ^1.6.4 1.7.0
Transitive dependencies
args 1.6.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test