I have a function that downloads .Z files using ftp. I want the function to be able to unzip those downloaded files and then delete the zipped versions. I have tried gunzip and unzip but I am not having any luck.
MATLAB: How to unzip .Z files
downloaded filesgunzipunzipzip
Related Solutions
You will need to use java if you wish to extract only specified files
You can refer to java documentations at the following link.
zipFilename = 'abc.zip';zipJavaFile = java.io.File(zipFilename);zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);% entries = zipFile.getEntries; % to get all entries then iterate
entry = zipFile.getEntry('abcfolder/abcfile.txt'); % empty if not found
if(~isempty(entry)) inputstream = zipFile.getInputStream(entry); outJavaFile = java.io.File(fullfile(pwd,'abcfolder/abcfile.txt')); outStream = java.io.FileOutputStream(outJavaFile); copier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier; copier.copyStream(inputstream, outStream); outStream.close();endzipFile.close();
The ability to load Java classes into MATLAB based on a Maven <dependency> definition is not available in MATLAB R2020b. You can however actually use Maven itself outside of MATLAB to have it download all dependencies for you or possibly even package the main Java package and all its dependencies into a single JAR-file which can then be added to MATLAB.
Download all dependencies
1. Download/Install Maven (and its dependencies like a supported JDK if needed), see:
https://maven.apache.org/
2. Create a pom.xml file for your "project", for example:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myProject</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>msal4j</artifactId> <version>1.8.1</version> </dependency> </dependencies></project>
Where you can further customize the properties of this project itself (although they are not really relevant in this workflow):\n
<groupId>com.example</groupId> <artifactId>myProject</artifactId> <version>1.0.0</version>
And you customize the <dependencies> to actually refer to the Java package which you want to load into MATLAB, in this example we refer to Microsoft's MSAL4J package:\n
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>msal4j</artifactId> <version>1.8.1</version> </dependency>
3. Now in the directory where you created your pom.xml run Maven's copy-dependencies command of the dependency plugin:\n
mvn dependency:copy-dependencies -DoutputDirectory=deps
Which will download all the dependencies into a directory named "deps". You can now add all the downloaded JAR-files to MATLAB's Java classpath.
Package the Java classes and all its dependencies into a single JAR
1. Download/Install Maven (and its dependencies like a supported JDK if needed), see:
https://maven.apache.org/
2. Create a pom.xml file for your "project", for example:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>msal4j</artifactId> <version>1.8.1</version> <dependencies> <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>msal4j</artifactId> <version>1.8.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build></project>
Where you can further customize the properties of this project itself (this will influence the name of the JAR-file which is produced):\n
<groupId>com.example</groupId> <artifactId>myProject</artifactId> <version>1.0.0</version>
And you customize the <dependencies> to actually refer to the Java package which you want to load into MATLAB, in this example we refer to Microsoft's MSAL4J package:\n
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>msal4j</artifactId> <version>1.8.1</version> </dependency>
3. Now in the directory where you created your pom.xml run:\n
mvn clean assembly:single
Which will download all the dependencies, clean the project and then assemble all relevant classes into a single JAR-file. You can then add this JAR-file to your MATLAB Java classpath.
Best Answer