Main Content

zip

Compress files into zip file

Description

example

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

entrynames = zip(___) returns a cell array of character vectors containing the names of the files included in zipfilename. You can use this syntax with any of the input argument combinations in the previous syntaxes.

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');

Input Arguments

collapse all

Name of zip file to create, specified as a character vector or string scalar. 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.

Data Types: char | string

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.

Data Types: char | string

Root folder, specified as a character vector or string scalar. 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.

Data Types: char | string

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.

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