Main Content

unzip

Extract contents of ZIP file

Description

unzip(zipfilename) extracts the archived contents of zipfilename into the current folder, preserving the attributes and timestamps of each file. unzip can extract files from your local system or from an internet URL.

If a file exists with the same name and the file is not read-only, MATLAB® overwrites it. Otherwise, MATLAB issues a warning. On Microsoft® Windows® platforms, the hidden, system, and archive attributes are not set.

example

unzip(zipfilename,outputfolder) extracts zipfilename into outputfolder. If outputfolder does not exist, MATLAB creates it.

example

unzip(___,Password=password) extracts the contents of a password-protected, encrypted, ZIP file using the specified password. You can specify this argument in addition to any of the input argument combinations in the previous syntaxes. (since R2024b)

example

filenames = unzip(___) returns a cell array of character vectors containing the names of the extracted files.

example

Examples

collapse all

Create a zip file and extract it to the folder archive.

Create the zip file examples.zip containing example MAT-files.

zip('examples.zip','*.mat',...
    fullfile(matlabroot,'toolbox','matlab','audiovideo'))

Extract examples.zip to the folder archive.

exampleFiles = unzip('examples.zip','archive')
exampleFiles = 1×7 cell
    {'archive\chirp.mat'}    {'archive\gong.mat'}    {'archive\handel.mat'}    {'archive\laughter.mat'}    {'archive\mtlb.mat'}    {'archive\splat.mat'}    {'archive\train.mat'}

Download and extract a ZIP file from a URL to a local folder.

Suppose you have the ZIP file example_file.zip stored at the URL http://example.com/example_file.zip. Download and extract the file to the desired local folder, example_folder.

url = 'http://example.com/example_file.zip';
unzip(url, 'example_folder');

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="aes-256");

Extract the password-protected ZIP file to a folder.

unzip("tmwlogo","ExampleFolder1",Password="PaSsWoRd123");

Input Arguments

collapse all

Name of ZIP file to extract from, specified as a string scalar or character vector. If zipfilename has no extension, MATLAB searches for zipfilename appended with .zip.

zipfilename must include a path relative to the current folder or an absolute path. If zipfilename is not a full path, unzip searches for the file in the current folder and along the MATLAB path.

If zipfilename is a URL, zipfilename must include the protocol type (for example, http://). MATLAB downloads the URL to the temporary folder on your system, and then it deletes the URL on cleanup.

Target folder for the extracted files, specified as a string scalar or character vector.

Since R2024b

Password of the ZIP file, specified as a string scalar or character vector. If the specified password does not match the expected password for the ZIP file, the unzip function returns an error.

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

Output Arguments

collapse all

Names of extracted files, returned as a cell array of character vectors. If outputfolder specifies a relative path, filenames contains relative paths. If outputfolder specifies an absolute path, filenames contains absolute paths.

Tips

  • To extract a ZIP file that contains non-7-bit ASCII characters, extract the file on a machine that has the appropriate language/encoding settings.

Version History

Introduced before R2006a

expand all