Date Modified Tags Java

Introduction

新的7z压缩解压文章

感谢群里小伙伴们的讨论,更新下7 zip jbinding的用法。

利用7-zip的开源项目7-zip-JBinding来解压缩多种压缩文件,而不是调用外部命令(比如win下调用winrar)。

java自带的解压模块可解压缩的压缩类型有限。

项目地址(sourceforge)

代码示例

package core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
/**利用7zbinding*/
public class UnZip {


    void extractile(String filepath){
         RandomAccessFile randomAccessFile = null;
            IInArchive inArchive = null;

            try {
                randomAccessFile = new RandomAccessFile(filepath, "r");
                inArchive = SevenZip.openInArchive(null, // autodetect archive type
                        new RandomAccessFileInStream(randomAccessFile));

                // Getting simple interface of the archive inArchive
                ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

                System.out.println("   Hash   |    Size    | Filename");
                System.out.println("----------+------------+---------");

                for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                    final int[] hash = new int[] { 0 };
                    if (!item.isFolder()) {
                        ExtractOperationResult result;

                        final long[] sizeArray = new long[1];
                        result = item.extractSlow(new ISequentialOutStream() {
                            public int write(byte[] data) throws SevenZipException {

                                //Write to file
                                FileOutputStream fos;
                                try {
                                    File file = new File(item.getPath());
                                    //error occours below
//                                  file.getParentFile().mkdirs();
                                    fos = new FileOutputStream(file);
                                    fos.write(data);
                                    fos.close();

                                } catch (FileNotFoundException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                hash[0] ^= Arrays.hashCode(data); // Consume data
                                sizeArray[0] += data.length;
                                return data.length; // Return amount of consumed data
                            }
                        });
                        if (result == ExtractOperationResult.OK) {
                            System.out.println(String.format("%9X | %10s | %s", // 
                                    hash[0], sizeArray[0], item.getPath()));
                        } else {
                            System.err.println("Error extracting item: " + result);
                        }
                    }
                }
            } catch (Exception e) {
                System.err.println("Error occurs: " + e);
                e.printStackTrace();
                System.exit(1);
            } finally {
                if (inArchive != null) {
                    try {
                        inArchive.close();
                    } catch (SevenZipException e) {
                        System.err.println("Error closing archive: " + e);
                    }
                }
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException e) {
                        System.err.println("Error closing file: " + e);
                    }
                }
            }
    }
}

调用的时候:

unzip=new UnZip();
unzip.extractile("a.7z");

会自动解压缩压缩包里的文件到当前目录下,当然可以更改设置,到特定的目录。代码简单明确。有问题可以到上面的sourceforge项目地址下的discuss搜索。


文章版权归 FindHao 所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权|
转载必须包含本声明,并以超链接形式注明作者 FindHao 和本文原始地址:
https://findhao.net/easycoding/1057.html

Comments