provide default CabStreamSaver

This commit is contained in:
Zhengyu 2018-04-12 00:15:02 +08:00
parent 6bae72b6ab
commit 98db0cd047
2 changed files with 80 additions and 4 deletions

View File

@ -15,10 +15,7 @@
*/
package dorkbox.cabParser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.Enumeration;
import dorkbox.cabParser.decompress.CabDecompressor;
@ -84,6 +81,15 @@ public final class CabParser {
readData();
}
public
CabParser(InputStream inputStream, File extractPath) throws CabException, IOException {
this.streamSaver = new DefaultCabStreamSaver(extractPath);
this.cabInputStream = new CabInputStream(inputStream);
readData();
}
/**
* Gets the version number.
*/

View File

@ -0,0 +1,70 @@
/*
* Copyright 2012 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.cabParser;
import dorkbox.cabParser.structure.CabFileEntry;
import java.io.*;
/**
* provide default CabStreamSaver
*/
public class DefaultCabStreamSaver implements CabStreamSaver{
private File extractPath;
public DefaultCabStreamSaver(File extractPath) throws CabException {
this.extractPath = extractPath;
if(extractPath!=null){
if(!extractPath.exists()){
extractPath.mkdirs();
}else if(!extractPath.isDirectory()){
throw new CabException("extractPath is not directory");
}
}
}
@Override
public OutputStream openOutputStream(CabFileEntry entry) {
return new ByteArrayOutputStream((int) entry.getSize());
}
@Override
public void closeOutputStream(OutputStream outputStream, CabFileEntry entry) {
if (outputStream != null) {
try {
ByteArrayOutputStream bos = (ByteArrayOutputStream)outputStream;
byte[] bytes=bos.toByteArray();
String filePath = extractPath.getAbsolutePath()+File.separator+entry.getName();
File cabEntityFile = new File(filePath);
cabEntityFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(cabEntityFile);
fileOutputStream.write(bytes);
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
}
@Override
public boolean saveReservedAreaData(byte[] data, int dataLength) {
return false;
}
}