Decompress the 7Zip special compression algorithm file and the 7zip Compression Algorithm in the Java project.

Source: Internet
Author: User

Decompress the 7Zip special compression algorithm file and the 7zip Compression Algorithm in the Java project.

1. Problem Description
Java Web backend downloads a zip file compressed by a special algorithm. Because java itself cannot be used to decompress the file, you must use 7Zip to decompress the file. Therefore, this article mentioned the problem of calling external 7zip exe on the java web backend to decompress the file.


2. Implement 2.1 to define the buffer class

class StreamGobbler extends Thread {    InputStream is;    String type;        public StreamGobbler(InputStream is, String type) {        this.is = is;        this.type = type;    }        public void run() {        try {            InputStreamReader isr = new InputStreamReader(is);            BufferedReader br = new BufferedReader(isr);            String line=null;            while ((line = br.readLine()) != null) {                System.out.println(type + ">" + line);            }        } catch (IOException ioe){            ioe.printStackTrace();          }    }}

2.2 execute the external exe process

String [] cmd = {"7za.exe", "x", zipPath, "-o" + outputPath}; Runtime rt = Runtime. getRuntime (); Process proc = rt.exe c (cmd); // listener error message StreamGobbler errorGobbler = new StreamGobbler (proc. getErrorStream (), "ERROR"); // listener output information StreamGobbler outputGobbler = new StreamGobbler (proc. getInputStream (), "OUTPUT"); // start the listener and input errorGobbler. start (); outputGobbler. start (); // make sure that the Runtime.exe c process has been executed. int exitVal = proc. waitFor (); System. out. println ("ExitValue:" + exitVal );



3. Solve the cause of 3.1 Process. waitFor stuck status 3.1.1 when running in tomcat.
Be sure to call Process. before waitFor (), read stdout and stderr of the program. Otherwise, the pipe buffer may be insufficient. The called system commands may be blocked on standard output and standard error output. Windows is more prone to this problem because the default value of this buffer zone is small.


Note that stdout and stderr of the read program are both blocked operations, which means that they must be read separately in two threads, rather than once in one thread. Otherwise, blocking may still occur.


[Http://www.dongliu.net/post/496142]


3.1.2 Other guesses
1. The main process of the program will wait for a certain amount of time for the process, but the time is very short. It may end when the process cannot complete the work. Therefore, the waitFor method needs to be called for a process that takes a long time to work. This method will cause the current Thread to wait until the process is interrupted. [Http://ccchhhlll1988-163-com.iteye.com/blog/4101497]


2. It may be because a process is started in tomcat, but you do not have the permission to kill the process.


4 others
4.1 decompress the library with Java
Java.util.zip


Due to inconsistent algorithms, the system prompts "invalid CEN header (bad compression method)" during decompression )"


4.2 download the 7Zip decompression Library
Download the sevenzipjbinding package from the sourceforge website. The downloaded files are not compressed.


However, the latest decompression algorithm cannot be canceled.


4.3 does tomcat have the permission to call external exe?
4.4 tomcat startup failed
The server can start the project normally, but the local computer cannot start. Message: "org. apache. catalina. LifecycleException: Failed to start component"


After the local tomcat6 test calls an external exe, you can check whether the project is successful in the actual project. The actual project uses tomcat7 and contains the corresponding jdk folder, some modifications were made. For example, you have modified the webapp folder path and deleted unnecessary files;


The final cause is that jdk of a local lower version is used at startup. Set JAVA_HOME to the built-in jdk folder.


5 References
1. waitfor usage in runtime.getruntime.exe c

During program design, the current thread can perform the next operation only after the called system program completes the operation. At this time, the Process-like method waitFor () can be used to implement the operation, it will block the first thread until the program running ends.



2. Call the system command correctly -- set timeout for Process. waitFor and other

Therefore, it is necessary to add timeout control to the code. However, Process. waitFor () does not support timeout settings,
One method is to use the non-blocking Process. exitValue () method, and then round-robin to check the Process status. This method consumes a lot of CPU, so that the polling interval cannot be set too small, which is always not perfect.
In addition, another thread is used to call the program. When timeout is found in the main thread, process. destroy () is called directly to terminate the process.

3. When Runtime.exe c () won't
This section describes why the Runtime function cannot be executed normally.




Java decompress the zip file

Sorry, it's easier.
Use this constructor ZipInputStream (InputStream in); receive the passed stream, decompress the file using the getNextEntry () method of this class, and finally call read (byte [] B, int off, int len) method to write data into the byte array.
ZipInputStream zin = new ZipInputStream (in );
ZipEntry entry = null;
While (entry = zin. getNextEntry ())! = Null ){
If (entry. isDirectory () | entry. getName (). equals ("..\\"))
Continue;
BufferedInputStream bin = new BufferedInputStream (zin );
Byte [] buf = new byte [];
Bin. read (buf, 0, 1 );
}

For java decompressed files (rar or zip), the compressed files are compressed according to the original directory of the files, not in the same folder.

NTFS is an advanced file system that provides security and reliability supported by WinNT and later versions. In Windows and Windows XP, NTFS also provides advanced features such as file and folder permissions, encryption, disk quotas, and compression.

1. encrypted files or folders

Step 1: open Windows Resource Manager.

Step 2: Right-click the file or folder to be encrypted and click "properties ".

Step 3: On the General tab, click Advanced ". Select the "encrypt content to protect data" check box

Pay attention to the following points during encryption:

1. To open "Windows Resource Manager", click "Start> program> attachment", and then click "Windows Resource Manager ".

2. Only files and folders on the NTFS partition volume can be encrypted. files and folders on the FAT partition volume are invalid.

3. the compressed file or folder can also be encrypted. If you want to encrypt a compressed file or folder, the file or folder will be decompressed.

4. Files marked as "system" attributes cannot be encrypted, and files in the systemroot directory structure cannot be encrypted.

5. When you encrypt a folder, the system will ask if you want to encrypt its subfolders at the same time. If you select Yes, its subfolders will also be encrypted, and all files and subfolders added to the folder will be automatically encrypted when they are added.

Ii. decrypt files or folders

Step 1: open Windows Resource Manager.

Step 2: Right-click the encrypted file or folder and click "properties ".

Step 3: On the General tab, click Advanced ".

Step 4: Clear the "encrypt content to protect data" check box.

Similarly, we should pay attention to the following issues during decryption:

1. To open "Windows Resource Manager", click "Start> program> attachment", and then click "Windows Resource Manager ".

2. When decrypting folders, the system will ask if you want to simultaneously decrypt all files and subfolders in the folder. If you select to decrypt Only folders, the encrypted files and subfolders in the folders to be decrypted are still encrypted. However, new files and folders created in the decrypted folder will not be automatically encrypted.

The above is how to use file encryption and decryption! During use, we may encounter the following problems:

1. the Advanced button is unavailable.

Cause: the Encrypted File System (EFS) can only process files and folders on the NTFS file system volume. If the file or folder you are trying to encrypt is on a FAT or FAT32 volume, the Advanced button does not appear in the properties of the file or folder.

Solution:

Converts a volume to an NTFS Volume with the conversion utility.

Open a command prompt. Type:

Convert [drive]/fs: ntfs

(Drive is the drive letter of the target drive)

2. When an encrypted file is opened, the message "Access Denied" is displayed.

Cause: the Encrypted File System (EFS) uses the Public Key Certificate to encrypt the file. The private key related to the certificate is unavailable on the computer.

Solution:

Find the private key of the appropriate certificate, and use the Certificate Management Unit to import the Private Key to the computer and use it on the local machine.

3. A solution to the problem that the encrypted file cannot be accessed after the system is reinstalled Based on NTFS (Note: before reinstalling Win2000/XP, you must back up the encrypted user certificate ):

Step 1: log on to the computer as an encrypted user.

Step 2: Click Start> Run, type mmc, and click OK ".

Step 3: on the "console" menu, click "Add/delete Management Unit", and then click "add ".

Step 4: Under Unit management, click certificate, and then click Add ".

Step 5: click "My User Account" and then click "finish" (2. This window will not appear if you encrypt the user as an administrator and go directly to the next step ).

Step 6: Click Close and then click OK ".

Step 7: Double-click "Certificate-current user", double-click "personal", and then double-click "certificate ".

Step 8: Click the certificate with the words "encrypted file" displayed in the "expected purpose" column.

Step 9: Right-click the certificate... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.