maja42 / ember

Embed arbitrary resources into a go executable at runtime, after the executable has been built.
MIT License
79 stars 7 forks source link

Zip archive not embedded completely #3

Closed mcfriend99 closed 1 month ago

mcfriend99 commented 1 month ago

I'm using the function below to embed:

func Embed(base string, destination string, attachments map[string]string) error {
    // Open executable
    exe, err := os.Open(base)
    if err != nil {
        return fmt.Errorf("Failed to open executable %q: %s", base, err)
    }
    defer exe.Close()

    // Open output
    out, err := os.OpenFile(destination, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0755)
    if err != nil {
        return fmt.Errorf("Failed to open output file %q: %s", destination, err)
    }
    defer func() {
        _ = out.Close()
        if err := recover(); err != nil { // execution failed; delete created output file
            _ = os.Remove(destination)
        }
    }()

    logger := func(format string, args ...interface{}) {
        fmt.Printf("\t"+format+"\n", args...)
    }

    embedding.SkipCompatibilityCheck = true
    return embedding.EmbedFiles(out, exe, attachments, logger)
}

The problem currently is that anytime I embed a zip archive, upon reading the zip archive has a size shorter than the original.

Upon inspection, I noticed that while my initial executable size was 3.1 mb before embedding a file of 45 mb, the final executable size read 46.8 mb. When extracted, the size of the zip file created is 43.7 mb which means there's a missing 1.3 mb in the archive written.

What could be causing this?? How do I resolve this?

Here's the code I'm using to extract:

if file, err := os.OpenFile(exe, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755); err == nil {
  if _, err := io.Copy(file, r); err == nil {
      fmt.Println("done")
  }
}

where r = attachments.Reader(name).

All helps appreciated.

maja42 commented 1 month ago

I'm using it to embed zip archives myself and never got any issues. I can check your code when I'm back on my PC. What happens if you embed another, smaller file than a zip archive? What parts are missing, or does this only happen once you reach a certain file size?

Any reason why you skip the compatibility check?

mcfriend99 commented 1 month ago

I've seen the problem and resolved it. It has nothing to do with ember. Also, I noticed some filetypes that have their own magic headers were having issues unless I skip the compatibility check.