ENESFRITEO
Archiving files in plain text

It happens that one wants to send a set of files by e-mail and keep the folder structure of the files being sent. This can be the case for sending files containing code in some programming language with interdependencies or a website with html, css and js files.

Several tools can be used for such task (e.g. zip or tar), but there are e-mail services that block any attachment with binary files. This is common in some companies with the purpose of preventing viruses and malware from entering their network. In such cases tar or zip files would get blocked since they are binary.

It is possible to create plain text archive files using the Unix commands diff and patch. They are commonly used to apply changes made to a common file in different places. diff saves the differences between the original file and the modified one while patch applies the differences to files with the same contents than the original one (you can find a tutorial about diff and patch in this link). On windows you can get these commands with cygwin or mingw. They are also included in the bash console provided by git for windows.

To exemplify the procedure, let us create a new folder, with two files and a sub-folder containing a third file with some text on each. The following commands create this structure:

  ## Create folder and subfolder
  mkdir -p testfolder/subfolder

  ## Create text files
  echo "This is file 1" > testfolder/file1.txt
  echo "This is file 2" > testfolder/file2.txt
  echo "This is file 3" > testfolder/subfolder/file3.txt

The idea is for diff to give us the differences between and empty folder and the folder we just created. So, it is first necessary to create an empty folder.

  mkdir testfolder_empty

Now we use diff to get the differences between the empty folder and the folder we want to archive. We store it in a file called testfolder.patch.

  diff -Nru testfolder_empty testfolder > testfolder.patch

testfolder.patch is our archive file in plain text. In order to "unarchive" it, we put it in a folder that does not contain another folder with the name testfolder and run

  patch -p0 < testfolder.patch
	      

At this point a folder called testfolder should appear containing all the files and sub-folders from the original one.

Retourner à la page principal