bashunzip

Unzipping a file without saving its contents


I am unzipping the mysql-api.jpi file with the following command:

unzip mysql-api.jpi

Here is the output I get:

Archive:  mysql-api.jpi
creating: META-INF/
inflating: META-INF/MANIFEST.MF    
creating: WEB-INF/
creating: WEB-INF/lib/
creating: META-INF/maven/
creating: META-INF/maven/io.jenkins.plugins/
creating: META-INF/maven/io.jenkins.plugins/mysql-api/
inflating: WEB-INF/lib/mysql-connector-j-8.4.0.jar  
inflating: WEB-INF/lib/mysql-api.jar  
inflating: WEB-INF/lib/protobuf-java-3.25.1.jar  
inflating: WEB-INF/licenses.xml    
inflating: META-INF/maven/io.jenkins.plugins/mysql-api/pom.xml  
inflating: META-INF/maven/io.jenkins.plugins/mysql-api/pom.properties

And then I need to copy one of the files to classpath:

cp WEB-INF/lib/mysql-connector-j-8.4.0.jar /var/lib/jenkins/war/WEB-INF/lib

Is there a way to directly unzip and copy without saving the files generated from unzipping?


Solution

  • Yes. Use the -p option to pipe the output to stdout. Then you can:

    unzip -p mysql-api.jpi WEB-INF/lib/mysql-connector-j-8.4.0.jar > /var/lib/jenkins/war/WEB-INF/lib/mysql-connector-j-8.4.0.jar
    

    That would be useful if you want to pipe the extracted data through some other processing. Since you are simply copying the file, which is in fact saving it, a more direct approach would be to specify the directory to extract to:

    unzip -d /var/lib/jenkins/war/ mysql-api.jpi WEB-INF/lib/mysql-connector-j-8.4.0.jar