top of page
frisemanicbloc

Zip a Directory Linux: The Ultimate Tutorial for Beginners and Experts



In one of our several articles about the tar command, we showed you how to extract tar files to a specific or different directory in Linux. This short guide explains to you how to extract/unzip .zip archive files to a specific or different directory in Linux.




How To Zip A Directory Linux



To create a .zip (packaged and compressed) file from the command line, you can run a similar command like the one below, The -r flag enables recursive reading of files directory structure.


To extract/unzip .zip archive files to specific or different directory from the command line, include the -d unzip command flag as shown below. We will use the same example above to demonstrate this.


1. Delete the archives inside the test_directory but keep the other files. To avoid confusion it would be good practice to delete the archives leaving only the original files that we created.


3. Create a ZIP archive containing the test_directory directory and its contents. Notice when you create the archive using the -r argument that you see a verbose output detailing each stage of the command as it descends into the directory and archives the files and folder.


We will create a ZIP archive called test_archive.zip which contains a directory test_directory which in turn contains 3 empty test files, test1.txt, test2.h, test3.c . To extract this archive we will use the unzip command.


2. Create a ZIP archive called test_archive.zip that contains the test_directory. Check that the archive has been successfully created using ls. The zip command has two arguments, the name of the archive that we wish to create, and the source of the files to be put into the archive.


3. Delete the un-archived directory. If we extract the test_archive.zip in the parent directory which contains the test_directory we would be asked if we want to replace or rename the archive.


1. Extract the test_archive.zip to a specified directory. We can specify a relative or absolute location. In this example we extract the contents to the Music directory using a relative path. We could also pass an absolute path which provides the full path to the target location.After extracting the archive, move into the specified directory and use ls to check the archive has been extracted.


With these few basic uses of the zip command you now have lots of options when creating ZIP archives in the linux terminal. Being able to select specific file types and being able to set the compression level in the terminal emulator gives quick access to these powerful tools that are often hard to find in a GUI application.


In this tutorial we will learn,how to zip the directory in linux with command line.Zip is a compression and file packaging utility for Linux,Unix,Windows and various Operating system. The zip helps to compress and reduce the size of file and directory.To zip a directory,first of all we will check ,do we have zip command installed in Linux system.


To add a new file to the existing ZIP file (newfiles.zip), run the following command. The command below updates (-u) the existing ZIP file package (newfiles.zip) by recursively (-r) searching through all files in the /mydir directory, then adding them to the ZIP package.


To unzip a ZIP file, launch your terminal and run the following command. Notice the command below will unzip the contents of the (newdir.zip) ZIP file. By default, the contents are extracted to the same directory (/mydir) of the (newdir.zip) ZIP file.


To demonstrate how to script some common use cases with the zip utility, perhaps you have a directory with many different files but only need to compress those that match a specific extension. To do that, you need a script that will repeatedly search for all files with the same extension. When found, adds them to a zip file.


The script then executes a for loop function to search all files with the .txt (files) extension inside the target directory (anotherDir). When a .txt file is found, the zip command adds (-u) that file to the (mynewfiles.zip) ZIP file. Ready to automate zipping files?


If you have a lot of files to upload, or if you need to preserve the directory structure (e.g. for a website or software code), then we strongly recommend creating a zip archive containing all the files.


I recommend using the recursive option -r as it allows you to add folders and their content. Without this option, you'll be adding the folder to the zipped file but the directory contents won't be included.


Execute the following to create a single .tar.bz2 file containing all of the contents of the specified directory compressed with BZIP. (BZIP typically produces smaller files than GZIP, at the cost of more processing time.):


If you'd rather specify a different directory to extract files to rather than just dumping everything in the current directory add -C /PATH/TO/DIRECTORY/ to the commands above. Replace /PATH/TO/DIRECTORY/ with the actual path to the directory where you want the files to be placed.


To create a zip archive of more than one file, pass them in a list (separated by space) after the zip filename. It is also good to ensure you have to write permissions in the directory you are creating the zip file.


A directory is placed at the end of a ZIP file. This identifies what files are in the ZIP and identifies where in the ZIP that file is located. This allows ZIP readers to load the list of files without reading the entire ZIP archive. ZIP archives can also include extra data that is not related to the ZIP archive. This allows for a ZIP archive to be made into a self-extracting archive (application that decompresses its contained data), by prepending the program code to a ZIP archive and marking the file as executable. Storing the catalog at the end also makes possible hiding a zipped file by appending it to an innocuous file, such as a GIF image file.


A ZIP file is correctly identified by the presence of an end of central directory record which is located at the end of the archive structure in order to allow the easy appending of new files. If the end of central directory record indicates a non-empty archive, the name of each file or directory within the archive should be specified in a central directory entry, along with other metadata about the entry, and an offset into the ZIP file, pointing to the actual entry data. This allows a file listing of the archive to be performed relatively quickly, as the entire archive does not have to be read to see the list of files. The entries within the ZIP file also include this information, for redundancy, in a local file header. Because ZIP files may be appended to, only files specified in the central directory at the end of the file are valid. Scanning a ZIP file for local file headers is invalid (except in the case of corrupted archives), as the central directory may declare that some files have been deleted and other files have been updated.


For example, we may start with a ZIP file that contains files A, B and C. File B is then deleted and C updated. This may be achieved by just appending a new file C to the end of the original ZIP file and adding a new central directory that only lists file A and the new file C. When ZIP was first designed, transferring files by floppy disk was common, yet writing to disks was very time-consuming. If you had a large zip file, possibly spanning multiple disks, and only needed to update a few files, rather than reading and re-writing all the files, it would be substantially faster to just read the old central directory, append the new files then append an updated central directory.


The ZIP format uses specific 4-byte "signatures" to denote the various structures in the file. Each file entry is marked by a specific signature. The end of central directory record is indicated with its specific signature, and each entry in the central directory starts with the 4-byte central file header signature.


Tools that correctly read ZIP archives must scan for the end of central directory record signature, and then, as appropriate, the other, indicated, central directory records. They must not scan for entries from the top of the ZIP file, because (as previously mentioned in this section) only the central directory specifies where a file chunk starts and that it has not been deleted. Scanning could lead to false positives, as the format does not forbid other data to be between chunks, nor file data streams from containing such signatures. However, tools that attempt to recover data from damaged ZIP archives will most likely scan the archive for local file header signatures; this is made more difficult by the fact that the compressed size of a file chunk may be stored after the file chunk, making sequential processing difficult.


This ordering allows a ZIP file to be created in one pass, but the central directory is also placed at the end of the file in order to facilitate easy removal of files from multiple-part (e.g. "multiple floppy-disk") archives, as previously discussed.


The original .ZIP format had a 4 GB (232 bytes) limit on various things (uncompressed size of a file, compressed size of a file, and total size of the archive), as well as a limit of 65,535 (216-1) entries in a ZIP archive. In version 4.5 of the specification (which is not the same as v4.5 of any particular tool), PKWARE introduced the "ZIP64" format extensions to get around these limitations, increasing the limits to 16 EB (264 bytes). In essence, it uses a "normal" central directory entry for a file, followed by an optional "zip64" directory entry, which has the larger fields.[39]


The format of the Local file header (LOC) and Central directory entry (CEN) are the same in ZIP and ZIP64. However, ZIP64 specifies an extra field that may be added to those records at the discretion of the compressor, whose purpose is to store values that do not fit in the classic LOC or CEN records. To signal that the actual values are stored in ZIP64 extra fields, they are set to 0xFFFF or 0xFFFFFFFF in the corresponding LOC or CEN record. If one entry does not fit into the classic LOC or CEN record, only that entry is required to be moved into a ZIP64 extra field. The other entries may stay in the classic record. Therefore, not all entries shown in the following table might be stored in a ZIP64 extra field. However, if they appear, their order must be as shown in the table. 2ff7e9595c


1 view0 comments

Recent Posts

See All

Comments


bottom of page