Search code examples
matlabfileunzip

How to extract a single file from a zip archive in MATLAB?


Single File unzip

The unzip() function in MATLAB only provides the ability to input a zip file location and an output directory location. Is there a way to extract only one file from the zip archive instead of all the files?

If the specific file is known and it is the only file needed, this will reduce the amount of time to extract the file.


Solution

  • MATLAB's unzip

    This is not possible with the standard MATLAB function, however...lets hack the function to make it do what is needed!

    MATLAB 's unzip Single File Hack

    Using the code from MATLAB's unzip() and extractArchive() (which is called from unzip()), a custom function may be created to only extract a single file from a zip archive.

    function [] = extractFile(zipFilename, outputDir, outputFile)
    % extractFile
    
    % Obtain the entry's output names
    outputName = fullfile(outputDir, outputFile);
    
    % Create a stream copier to copy files.
    streamCopier = ...
        com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
    
    % Create a Java zipFile object and obtain the entries.
    try
        % Create a Java file of the Zip filename.
        zipJavaFile = java.io.File(zipFilename);
    
        % Create a java ZipFile and validate it.
        zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
    
        % Get entry
        entry = zipFile.getEntry(outputFile);
    
    catch exception
        if ~isempty(zipFile)
            zipFile.close;
        end
        delete(cleanUpUrl);
        error(message('MATLAB:unzip:unvalidZipFile', zipFilename));
    end
    
    % Create the Java File output object using the entry's name.
    file = java.io.File(outputName);
    
    % If the parent directory of the entry name does not exist, then create it.
    parentDir = char(file.getParent.toString);
    if ~exist(parentDir, 'dir')
        mkdir(parentDir)
    end
    
    % Create an output stream
    try
        fileOutputStream = java.io.FileOutputStream(file);
    catch exception
        overwriteExistingFile = file.isFile && ~file.canWrite;
        if overwriteExistingFile
            warning(message('MATLAB:extractArchive:UnableToOverwrite', outputName));
        else
            warning(message('MATLAB:extractArchive:UnableToCreate', outputName));
        end
        return
    end
    
    % Create an input stream from the API
    fileInputStream = zipFile.getInputStream(entry);
    
    % Extract the entry via the output stream.
    streamCopier.copyStream(fileInputStream, fileOutputStream);
    
    % Close the output stream.
    fileOutputStream.close;
    
    end