首页 > 解决方案 > Simplest format to archive a directory

问题描述

I have a script that needs to work on multiple platforms and machines. Some of those machines don't have any available archiving software (e.g. zip, tar). I can't download any software onto these machines.

The script creates a directory containing output files. I need to package all those files into a single file so i can download it easily.

What is the simplest possible archiving format to implement, so I can easily roll my own impl in the script. It doesn't have to support compression.

I could make up something ad-hoc, e.g.

file1  base64EncodedContents
dir1/file1 base64EncodedContents

etc.

However if one already exists then that will save me having to roll my own packing and unpacking, only packing, which would be nice. Bonus points if it's zip compatible, so that I can try zipping it with compression if possible, and them impl my own without compression otherwise, and not have to worry about which it is on the other side.

标签: ziparchive

解决方案


The tar archive format is extremely simple - simple enough that I was able to implement a tar archiver in powershell in a couple of hours.

It consists of a sequence of file header, file data, file header, file data etc.

The header is pure ascii, so doesn't require any bit manipulation - you can literally append strings. Once you've written the header, you then append the file bytes, and pad it with nil chars till it's a multiple of 512 bytes. You then repeat for the next file.

Wikipedia has more details on the exact format: https://en.wikipedia.org/wiki/Tar_(computing).


推荐阅读