Narayana Tutorial

Online Java Tutorial Blog

7-Zip Command-Line Examples In Java

7-Zip Command-Line Examples In Java

The 7za.exe program is used to compress, extract and update files through the command line. It provides superior compression. It is a great program. The 7za.exe program commands will execute through java to make files compress with password protected and without password protected, extract and update the files in the archive.

Here I am going sharing some command lines to compress the files then we can understand easily execute the same commands through java.

7zip installed directory is C:/Program Files/7-Zip/

To compress files

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z a -t7z D:/narayanatutorial/files.7z D:/narayanatutorial/*.txt

Description
 7z :- use this executable
 a :-  archive or add. Use it to put the files in an archive
 t7z:- traverse and output folder extension is .7z .i.e files.7z
 D:/narayanatutorial/files.7z :- output archive folder
 D:/narayanatutorial/*.txt:- input files with extension.txt

Different types of switches for archive type

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z a D:/narayanatutorial/files.<extension> D:/narayanatutorial/*.txt

Description
Switch: -t7z
Format: 7Z
extension: archive.7z (default option)

Switch: -tgzip
Format: GZIP
extension: archive.gzip, archive.gz

Switch: -tzip
Format: ZIP
extension: archive.zip (compatible)

Switch: -tbzip2
Format: BZIP2
extension: archive.bzip2

Switch: -ttar
Format: TAR
extension: tarball.tar (UNIX and Linux)

Switch: -tudf
Format: UDF
extension: disk.udf

To compress files with password protected

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z a -t7z D:/narayanatutorial/files.7z D:/narayanatutorial/*.txt -pSECRET

Description
-p :- specify the password .i.e SECRET

To extract files from password protected archive

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z x D:/narayanatutorial/files.7z

Description
x :- extract everything from archive into preserved full path and prompt for the password to enter

To extract files from archive to specified folder

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z e archive.zip

Description
e :- extract everything from archive into specified folder

To extract files from archive to preserved full path

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z x archive.zip

Description
x :- extract everything from archive into preserved full path

To list the content of archive

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z l archive.zip

Description
l :- list the content of archive

To test the integrity of archives

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z t archive.zip *.txt -r

Description
t :- test the specified archive
archive.zip :- the archive you want to test
*.txt :- test all these files in the archive
-r :- recurse all child directories

To replace old files in archives

cd C:/Program Files/7-Zip
C:/Program Files/7-Zip> 7z u archive.zip *.txt

Description
u :- update command
archive.zip :- archive you want to update files in
*.txt :- only update these files (text documents)

Here I am writing java program to create archive with and without password protected by using the above 7z.exe.

7-Zip Command-Line Examples In Java

package com.narayanatutorial.compress;

import java.io.IOException;
import java.io.InputStream;
/**
 * @author narayanaswamy
 */
public class CompressFile {
    public static void main(String[] args) {
        String source_file = "D:/narayanatutorial/file1.txt";
        String output_zip_without_password = "D:/file1_without_password.zip";
        String output_zip_with_password = "D:/file1_with_password.zip";
        String installed_path_7zip = System.getenv("ProgramFiles") + "\7-Zip\7z.exe";
        String password = "narayanatutorial";
        ProcessBuilder pb = null;
// without password
        pb = new ProcessBuilder(
                installed_path_7zip,
                "a",  //archive
                "-tzip",  // zip format
                output_zip_without_password,  // output without password zip folder
                source_file //input file
        );
        pb.redirectError();
        try {
            Process p = pb.start();
            new Thread( new CompressFile.InputConsumer( p.getInputStream() )).start();
            System.out.println("Exited with: " + p.waitFor());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
// with password protected
        pb = new ProcessBuilder(
                installed_path_7zip,
                "a",  //archive
                "-tzip",  //zip format
                output_zip_with_password,  // output with password zip folder
                source_file,  //input file
                "-p" + password  //password
        );
        pb.redirectError();
        try {
            Process p = pb.start();
            new Thread( new CompressFile.InputConsumer( p.getInputStream() )).start();
            System.out.println("Exited with: " + p.waitFor());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static class InputConsumer implements Runnable {
        private InputStream is;
        public InputConsumer(InputStream is) {
            this.is = is;
        }
        @Override
        public void run() {
            try {
                int value = -1;
                while ((value = is.read()) != -1) {
                    System.out.print((char) value);
                }
            } catch (IOException exp) {
                exp.printStackTrace();
            }
            System.out.println("output stream completed");
        }
    }
}
Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development.

I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply

Narayana Tutorial © 2018 Frontier Theme
Show Button
Hide Button