How can I put a tar file inside tar file in golang

supriya :

I want to create a tar file and inside that tar file I want to put other tar files.Hierarchy is something like

TopLevelTar.tar.gz
    |-->primary/primary.tar
    |-->secondary/secondaty.tar
    |-->tertiary/tertiary.tar

How can I do it in golag?

shivendra pratap singh :

I think this might help you. Basically, this solution is creating a tarball from multiple files. You have to simply give path of your files in place of a.go and b.go etc.

package main

import (
    "archive/tar"
    "compress/gzip"
    "io"
    "log"
    "os"
)

func addFile(tw *tar.Writer, path string) error {
    file, err := os.Open(path)
    if err != nil {
        return err
    }
    defer file.Close()
    if stat, err := file.Stat(); err == nil {
        // now lets create the header as needed for this file within the tarball
        header := new(tar.Header)
        header.Name = path
        header.Size = stat.Size()
        header.Mode = int64(stat.Mode())
        header.ModTime = stat.ModTime()
        // write the header to the tarball archive
        if err := tw.WriteHeader(header); err != nil {
            return err
        }
        // copy the file data to the tarball
        if _, err := io.Copy(tw, file); err != nil {
            return err
        }
    }
    return nil
}

func main() {
    // set up the output file
    file, err := os.Create("output.tar.gz")
    if err != nil {
        log.Fatalln(err)
    }
    defer file.Close()
    // set up the gzip writer
    gw := gzip.NewWriter(file)
    defer gw.Close()
    tw := tar.NewWriter(gw)
    defer tw.Close()
    // grab the paths that need to be added in
    paths := []string{
        "a.go",
        "b.go",
    }
    // add each file as needed into the current tar archive
    for i := range paths {
        if err := addFile(tw, paths[i]); err != nil {
            log.Fatalln(err)
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to put folder into a tar file with gradle

From Dev

How can I rename files within a tar.gz file during extraction of the tar.gz file?

From Dev

How can I filter the contents of a tar file, producing another tar file in the pipe?

From Linux

How can I tar gz files with file name date range?

From Java

How can I untar a tar.bz file in unix?

From Dev

How can I update a tar.gz file?

From

How can I decompress an archive file having tar.zst?

From Dev

How can I convert a tar encoded file into a terraform variable?

From Dev

How can I check if .tar (not tar.gz) file is corrupt or not, in Ubuntu?

From Dev

Can I decompress a .gzip file with tar?

From Dev

How to list the folders/files of a file.tar.gz file inside a file.tar

From Java

How do I extract a tar file in Java?

From

how to write a directory [not just the files in it ] to a tar.gz file in golang

From Dev

How to Copy and Extract a .tar file inside a Docker Container

From Dev

How to verify a tar file in Windows?

From Dev

how to check if tar file is corrupted

From Dev

How to install tar file "globally"?

From

Golang: append file to an existing tar archive

From Dev

How do I use gunzip and tar to extract my tar.gz file to the specific directory I want?

From Dev

Extract Tar File inside Memory Filesystem

From Dev

modify file ownership for files inside tar archive

From Dev

How can I put .bak file inside my Android Application?

From Dev

How can I extract a tar.gz file in a Google Cloud Storage bucket from a Colab Notebook?

From Dev

How can I download OpenJDK 1.8 in a Centos machine and use the tar file to install Java in another Centos Machine

From Dev

how can i install adobe flash player 11.tar.gz file on ubuntu 13.04

From Java

How can I read tar.gz file using pandas read_csv with gzip compression option?

From Dev

How can i view the contents of a tar.gz file (filenames + filesize)

From Dev

How can I find out what is contained in a saved Docker image tar file?

From Dev

How can I view the contents of tar.gz file without extracting from the command-line?

Related Related

  1. 1

    how to put folder into a tar file with gradle

  2. 2

    How can I rename files within a tar.gz file during extraction of the tar.gz file?

  3. 3

    How can I filter the contents of a tar file, producing another tar file in the pipe?

  4. 4

    How can I tar gz files with file name date range?

  5. 5

    How can I untar a tar.bz file in unix?

  6. 6

    How can I update a tar.gz file?

  7. 7

    How can I decompress an archive file having tar.zst?

  8. 8

    How can I convert a tar encoded file into a terraform variable?

  9. 9

    How can I check if .tar (not tar.gz) file is corrupt or not, in Ubuntu?

  10. 10

    Can I decompress a .gzip file with tar?

  11. 11

    How to list the folders/files of a file.tar.gz file inside a file.tar

  12. 12

    How do I extract a tar file in Java?

  13. 13

    how to write a directory [not just the files in it ] to a tar.gz file in golang

  14. 14

    How to Copy and Extract a .tar file inside a Docker Container

  15. 15

    How to verify a tar file in Windows?

  16. 16

    how to check if tar file is corrupted

  17. 17

    How to install tar file "globally"?

  18. 18

    Golang: append file to an existing tar archive

  19. 19

    How do I use gunzip and tar to extract my tar.gz file to the specific directory I want?

  20. 20

    Extract Tar File inside Memory Filesystem

  21. 21

    modify file ownership for files inside tar archive

  22. 22

    How can I put .bak file inside my Android Application?

  23. 23

    How can I extract a tar.gz file in a Google Cloud Storage bucket from a Colab Notebook?

  24. 24

    How can I download OpenJDK 1.8 in a Centos machine and use the tar file to install Java in another Centos Machine

  25. 25

    how can i install adobe flash player 11.tar.gz file on ubuntu 13.04

  26. 26

    How can I read tar.gz file using pandas read_csv with gzip compression option?

  27. 27

    How can i view the contents of a tar.gz file (filenames + filesize)

  28. 28

    How can I find out what is contained in a saved Docker image tar file?

  29. 29

    How can I view the contents of tar.gz file without extracting from the command-line?

HotTag

Archive