国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 實例展示使用Java壓縮和解壓縮7z文件的方法

實例展示使用Java壓縮和解壓縮7z文件的方法

2020-01-16 16:22FIND JAVA教程

這篇文章主要介紹了實例展示使用Java壓縮和解壓縮7z文件的方法,用到了7-zip的開源項目7-zip-JBinding,需要的朋友可以參考下

壓縮7z文件
首先網絡上對7z的壓縮內容很少。
尤其是java調用進行壓縮的是更少了。
一下是自己完成的一個壓縮。
本人進行了測試是成功的。
將壓縮的流寫如磁盤一個壓縮文件中。
然后使用7z的壓縮軟件進行打開解壓。

7-zip的開源項目7-zip-JBinding項目地址(sourceforge)

不多說,調用7z源碼進行壓縮的方法如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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自帶的解壓模塊可解壓縮的壓縮類型有限。
代碼示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
          }
        }
      }
  }
}

調用的時候:

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

會自動解壓縮壓縮包里的文件到當前目錄下,當然可以更改設置,到特定的目錄。代碼簡單明確。有問題可以到上面的sourceforge項目地址下的discuss搜索。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久蜜桃精品一区二区三区综合网 | 羞羞视频在线 | 精品日韩一区二区 | 色天天综合 | 视频1区| 九色在线 | 美女毛片 | 狠久久| 成人影院av | 91精品久久 | 91在线你懂的| 九色在线视频 | 91电影在线看 | 91亚洲精品| jizz欧美大片 | 亚洲电影二区 | 玖玖国产精品视频 | 欧美日韩激情一区二区三区 | 中文字幕日韩欧美 | 亚洲精品久久久一区二区三区 | 久久伊人精品网 | 免费成人在线观看 | 成人精品一区二区 | 精品二区 | 久久久精品呻吟 | 黄色一级片在线观看 | 精品三级| 午夜电影 | 国产精品99久久久久久动医院 | 国产精品久久久久久中文字 | 亚洲天堂一区 | 欧美日韩久 | 涩涩视频观看 | 日韩一二三区视频 | 综合精品 | 亚洲一区免费观看 | 日韩在线视频中文字幕 | 久久精品视频免费 | 亚洲精品一区二区三区在线观看 | 日韩久色 | 日韩免费在线 |