public class UnZip7 {
public static void main(String args[]){
UnZip7 unzip=new UnZip7();
boolean flag=unzip.extractile("D://item.zip");
if(flag){
System.out.println("解壓成功。");
}
}
boolean extractile(String filepath){
RandomAccessFile randomAccessFile = null;
ISevenZipInArchive inArchive = null;
boolean flag=true;
try {
randomAccessFile = new RandomAccessFile(filepath, "r");
inArchive = SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile));
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
System.out.println(" Hash | Size | Filename");
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("D://7zip//"+item.getPath());
fos = new FileOutputStream(file);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
hash[0] ^= Arrays.hashCode(data);
sizeArray[0] += data.length;
return data.length;
}
});
if (result != ExtractOperationResult.OK) {
flag=false;
System.err.println("Error extracting item: " + result);
} else {
System.out.println(String.format("%9X |%10s | %s",
hash[0], sizeArray[0], item.getPath()));
}
}
}
} 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);
}
}
}
return flag;
}
}dom