2016-06-02 13 views
2

Single File unzip

Die unzip() Funktion in MATLAB nur die Fähigkeit zur Eingabestelle eine Zip-Datei Standort und ein Ausgabeverzeichnis zur Verfügung stellt. Gibt es eine Möglichkeit, nur eine Datei aus dem Zip-Archiv statt aller Dateien zu extrahieren?Wie extrahiere ich eine einzelne Datei aus einem Zip-Archiv in MATLAB?

Wenn die spezifische Datei bekannt ist und es die einzige benötigte Datei ist, reduziert dies die Zeit für das Extrahieren der Datei.

Antwort

3

MATLAB unzip

Das mit dem standard MATLAB function nicht möglich ist, aber ... lässt die Funktion hacken, um es zu tun, was notwendig ist!

MATLAB ‚s unzip Single File Hack

Verwenden Sie den Code aus MATLAB unzip() und extractArchive() (die von unzip() genannt wird), eine benutzerdefinierte Funktion erstellt werden, um nur eine einzelne Datei aus einem Zip-Archiv zu extrahieren.

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 
+0

Ich würde dies nicht einen "Hack" nennen, es ist eine ganz normale Java-Lösung. Ich habe fast die gleiche Funktion in der Vergangenheit geschaffen, um die benötigte Funktionalität zu erreichen (danach habe ich beschlossen, 7Zip binary in das Projekt zu integrieren und das via Systemaufruf zu benutzen) :) – DVarga

+0

Zustimmen, das ist vielleicht nicht revolutionär oder hacky in Bezug auf a volle Sprache wie Java, aber von einem MATLAB-only-Standpunkt aus ist es ein Hack ihres Codes (die vorhandene Schnittstelle/Infrastruktur mit nichts Neuem wurde einfach wiederverwendet und neu angeordnet, um etwas zu tun, was nicht beabsichtigt war). – tmthydvnprt