Skip to content

Instantly share code, notes, and snippets.

@s1037989
Last active February 18, 2021 02:47
Show Gist options
  • Select an option

  • Save s1037989/74bc5b0d5bd4e1da409be8850456e1a7 to your computer and use it in GitHub Desktop.

Select an option

Save s1037989/74bc5b0d5bd4e1da409be8850456e1a7 to your computer and use it in GitHub Desktop.
pack non-binary files into a single text file, and unpack them again
txtpack() {
if test -d "$1"/.git; then
cd "$1"
local tmp=$(mktemp -t "$1.$(git rev-parse --short HEAD).XXXX")
cd -
else
local tmp=$(mktemp -t "$1.xxxxxxxxx.XXXX")
fi
for i in $(find "$1" ! -path "*/.git/*" -type f); do
if iconv -f utf-8 -t utf-8 "$i" > /dev/null 2>&1; then
echo "$i"
cat "$i"
echo -en "\x00"
else
echo "$i" >&2
echo "$i"
echo -en "\x00"
fi
done > "$tmp"
echo "$tmp"
}
txtunpack() {
local tmpd=$(mktemp -d);
perl -MFile::Path=make_path -MFile::Basename=dirname,basename -E '$/="\x00"; my $tmpd = $ARGV[1]; open PACK, $ARGV[0]; while (<PACK>) { chomp; my ($file, $contents) = split /\n/, $_, 2; make_path($tmpd."/".dirname($file)); open FILE, ">$tmpd/$file"; print FILE $contents; close FILE; } close PACK' "$1" "$tmpd";
echo "$tmpd";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment