Main Content

zip

Compress files into ZIP file

Description

zip(zipfilename,filenames) compresses the contents of filenames into the ZIP file zipfilename. zip recursively compresses the content in folders. The resulting ZIP file contains the paths of filenames relative to the current folder. The ZIP file does not store absolute paths.

example

zip(zipfilename,filenames,rootfolder) specifies the paths for filenames relative to rootfolder rather than the current folder.

example

zip(___,Password=password,EncryptionMethod=encryptionmethod) creates a password-protected, encrypted, ZIP file using the specified password and encryption method. zip does not encrypt an empty file. You can specify these arguments in addition to any of the input argument combinations in the previous syntaxes. (since R2024b)

example

entrynames = zip(___) returns a cell array of character vectors containing the names of the files included in zipfilename.

example

Examples

collapse all

Create a zip file of the file membrane.m. Save the zip file tmwlogo.zip in the current folder.

zip('tmwlogo','membrane.m');

Compress the files membrane.m and logo.m into a file named tmwlogo.zip.

zippedfiles = zip('tmwlogo.zip',{'membrane.m','logo.m'})
zippedfiles = 1x2 cell
    {'membrane.m'}    {'logo.m'}

Compress all .m and .mlx files in the current folder to the file backup.zip.

zip('backup',{'*.m','*.mlx'});

Compress the contents of a folder including all subfolders, and store the relative paths in the zip file.

Create a folder myfolder containing a subfolder mysubfolder and the files membrane.m and logo.m.

mkdir myfolder;
movefile('membrane.m','myfolder');
movefile('logo.m','myfolder');
cd myfolder;
mkdir mysubfolder;
cd ..

Compress the contents of myfolder, including all subfolders.

zippedfiles = zip('myfiles.zip','myfolder');

Suppose that you have the files thesis.doc and defense.ppt located in the folder d:/PhD. Compress these files into thesis.zip, one level up from the current folder.

zip('../thesis.zip',{'thesis.doc','defense.ppt'},'d:/PhD');

Create a ZIP file of the file membrane.m. Save the ZIP file tmwlogo.zip in the current folder. Protect the file with a password, and specify an encryption method.

zip("tmwlogo","membrane.m",Password="PaSsWoRd123", ...
    EncryptionMethod="zipcrypto");

Input Arguments

collapse all

Name of ZIP file to create, specified as a string scalar or character vector. If zipfilename does not have a .zip extension, MATLAB® appends the .zip extension.

zipfilename must include a path relative to the current folder or an absolute path.

Names of files or folders to compress, specified as a character vector, a cell array of character vectors, or a string array.

Files that are on the MATLAB path can include a partial path. Otherwise, files must include a path relative to the current folder or an absolute path.

Folders must include a path relative to the current folder or an absolute path. On UNIX® systems, folders also can start with ~/ or ~username/, which expands to the current user's home folder or the specified user's home folder, respectively. You can use the wildcard character * when specifying files or folders, except when relying on the MATLAB path to resolve a file name or partial path name.

Root folder, specified as a string scalar or character vector. When file names are specified as relative paths, the root folder is used as the parent path to determine names of files to compress. By default, the root folder is the current working directory.

Since R2024b

Password for the ZIP file, specified as a string scalar or character vector. A password of 20 or more characters is recommended.

To increase security, avoid hard-coding sensitive information, such as passwords. For more information, see Keep Sensitive Information Out of Code.

Since R2024b

Encryption method, specified as a one of these values:

  • "aes-256" – Industry standard encryption. This method is recommended for most use cases.

  • "aes-128" – This method is faster than AES-256 and requires less computational power and memory. While AES-128 is a strong encryption method, it is more vulnerable to brute-force techniques compared to AES-256.

  • "zipcrypto" – This method creates a ZIP file that is compatible with most ZIP archivers. You must use this method if running MATLAB in a Linux® environment.

The zip function encrypts only the content of files and not the names of files and folders.

Output Arguments

collapse all

Names of compressed files, returned as a cell array of character vectors. Each element in entrynames is the path of an entry relative to the archive.

Limitations

  • On Linux systems, AES-128 and AES-256 encryption methods are not supported.

Alternative Functionality

To ZIP files in the Current Folder browser, select the file, right-click to open the context menu, and then select Create Zip File.

Version History

Introduced before R2006a

expand all