Author: Smin Rana

  • Create zip file in dart

    Create zip file in dart

    Creating a zip file in Dart is not that hard. Though there are not many packages right now to work with, so far we have only two packages which we can use to create zip, tar and others in dart.

    If you have not installed dart yet please go to my post here.

    Visit Pub Dev where you can find all kinds of packages for Flutter and Dart. Some packages work with both Flutter and Dart but not all of them.

    Go to https://pub.dev/ and search zip you will find these two packages

    1. Archive https://pub.dev/packages/archive, for Dart and Flutter.
    2. Flutter Archive https://pub.dev/packages/flutter_archive for only Flutter project. 

    In our case we will use the Archive package since we are testing in a console application. 

    The Archive library currently supports the following decoders:

    • Zip (Archive)
    • Tar (Archive)
    • ZLib [Inflate decompression]
    • GZip [Inflate decompression]
    • BZip2 [decompression]

    Add Archive package in your project 

    With Dart

    dart pub add archive

    With Flutter

    flutter pub add archive

    Our pubspec.yaml

    name: Create zip file in dart
    description: A sample command-line application to create zip file
    version: 1.0.0
    homepage: https://flutterframework.com/
    
    environment:
      sdk: '>=2.10.0 <3.0.0'
    
    dependencies:
      archive: ^3.1.2
    
    dev_dependencies:
      pedantic: ^1.9.0
      test: ^1.14.4

    Our main zip.dart

    import 'package:zip/zip.dart' as zip;
    
    void main(List<String> arguments) {
      zip.createZip();
    }

    and lib/zip.dart

    import 'dart:io';
    
    import 'package:archive/archive.dart';
    import 'package:archive/archive_io.dart';
    
    void createZip() {
      final logDir = Directory.current.path + '/log';
      final zipLocation = Directory.current.path + '/log.zip';
    
      var encoder = ZipFileEncoder();
      encoder.zipDirectory(Directory(logDir), filename: zipLocation);
    
      // Manually create a zip of a directory and individual files.
      encoder.create(Directory.current.path + '/log2.zip');
      encoder.addFile(File('another_file.txt'));
    
      encoder.close();
    }

    Line number 10 creates a zip file in the destination file(line 8), from line 7 which is a directory containing few txt files.

    You can create more zip file in the same encoder process, line 16 and more file line 17.

    Download the source code from github.com

    Spread the love
  • Git worktree example

    Git worktree example

    Git worktree is great a way to separate your code without having to create a new branch when you are not yet ready to commit.

    I have created a new repo in GitHub and cloned in a dir ‘worktree-example’.

    git clone [email protected]:sminrana/git-worktree-example.git

    Open the dir with code . command and update the readme.md file. Now we can push it to main branch.

    git add README.md 
    git commit -m "message"
    git push origin main

    It’s time to create our first worktree. We will put our worktree one dir up from our current dir where our main branch is. The documentation says -d flag with single hyphen but two hypen worked for me.

    git worktree add --d ../test 

    Now open test folder and open it in code . and make some changes in readme file then commit. Since worktree is detached HEAD you can’t commit to anywhere, for this we have create a new branch from here, run following command.

    git switch -c dev
    git push origin dev

    You can remove worktree

    git worktree remove test

    and remove completely from ref.

    git worktree prune

    That’s all you needed. More git worktree commands

    git worktree add --d ../hotfix 
    git worktree remove hotfix
    git worktree prune
    
    Throwaway working tree
    git worktree add --d <path>

    Download source code

    Spread the love