当前位置: 移动技术网 > IT编程>开发语言>Java > 实例展示使用Java压缩和解压缩7z文件的方法

实例展示使用Java压缩和解压缩7z文件的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

压缩为7z文件
首先网络上对7z的压缩内容很少。
尤其是java调用进行压缩的是更少了。
一下是自己完成的一个压缩。
本人进行了测试是成功的。
将压缩的流写如磁盘一个压缩文件中。
然后使用7z的压缩软件进行打开解压。

7-zip的开源项目7-zip-jbinding

不多说,调用7z源码进行压缩的方法如下。

public byte[] lzmazip(string xml) throws ioexception{ 
  bufferedinputstream instream = new bufferedinputstream(new bytearrayinputstream(xml.getbytes())); 
  bytearrayoutputstream bos = new bytearrayoutputstream(); 
   
  boolean eos = true; 
    encoder encoder = new encoder(); 
    encoder.setendmarkermode(eos); 
    encoder.writecoderproperties(bos); 
    long filesize = xml.length(); 
    if (eos) 
      filesize = -1; 
    for (int i = 0; i < 8; i++) 
      bos.write((int)(filesize >>> (8 * i)) & 0xff); 
    encoder.code(instream, bos, -1, -1, null); 
    return bos.tobytearray() ; 
} 

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

java自带的解压模块可解压缩的压缩类型有限。
代码示例

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.isevenzipinarchive;
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;
      isevenzipinarchive 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搜索。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网