下面列出了java.util.zip.ZipInputStream#available ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void extractZip(String dest, List<String> relativePaths, ZipInputStream zipInputStream)
throws IOException {
ZipEntry nextEntry;
zipInputStream.available();
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
String entryName = nextEntry.getName();
String fileName = dest + "/" + entryName;
File localFile = new File(fileName);
if (nextEntry.isDirectory()) {
localFile.mkdir();
} else {
relativePaths.add(entryName);
localFile.createNewFile();
FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
IOUtils.copyLarge(zipInputStream, localFileOutputStream, 0, nextEntry.getSize());
localFileOutputStream.flush();
localFileOutputStream.close();
}
zipInputStream.closeEntry();
}
zipInputStream.close();
LOGGER.info("File download complete!");
}
/**
* Get an inputstream of a GPX file inside a zip
* @param inFile File object describing zip file
* @return input stream for Xml parser
*/
private static InputStream getZipInputStream(File inFile)
{
try
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
while (zis.available() > 0)
{
ZipEntry entry = zis.getNextEntry();
String entryName = entry.toString();
if (entryName != null && entryName.length() > 4)
{
String suffix = entryName.substring(entryName.length()-4).toLowerCase();
if (suffix.equals(".gpx") || suffix.equals(".xml")) {
// First matching file so must be gpx
return zis;
}
}
}
}
catch (Exception e) {} // ignore errors
// not found - error!
return null;
}
/**
* @param zipInputStream zipInputStream
* @return return zipetry map
* @throws IOException IOException
*/
private static List<ZipEntry> populateList(ZipInputStream zipInputStream) throws IOException {
List<ZipEntry> listEntry = new ArrayList<ZipEntry>();
while (zipInputStream.available() == 1) {
ZipEntry entry = zipInputStream.getNextEntry();
if (entry == null) {
break;
}
listEntry.add(entry);
}
return listEntry;
}
/** Search Archive for classes.
* @param the archive file location
* @return array of class names found
* @throws IOException */
static String [] searchArchiveForClasses( URL url ) throws IOException {
List<String> list = new ArrayList<>();
ZipInputStream zip = new ZipInputStream(url.openStream());
ZipEntry ze;
while( zip.available() == 1 )
if ( (ze = zip.getNextEntry()) != null
&& isClassFileName( ze.getName() ) )
list.add( canonicalizeClassName( ze.getName() ) );
zip.close();
return list.toArray( new String[list.size()] );
}
public void test_available() throws Exception {
File resources = Support_Resources.createTempFolder();
Support_Resources.copyFile(resources, null, "hyts_ZipFile.zip");
File fl = new File(resources, "hyts_ZipFile.zip");
FileInputStream fis = new FileInputStream(fl);
ZipInputStream zis1 = new ZipInputStream(fis);
ZipEntry entry = zis1.getNextEntry();
assertNotNull("No entry in the archive.", entry);
long entrySize = entry.getSize();
assertTrue("Entry size was < 1", entrySize > 0);
int i = 0;
while (zis1.available() > 0) {
zis1.skip(1);
i++;
}
if (i != entrySize) {
fail("ZipInputStream.available or ZipInputStream.skip does not " +
"working properly. Only skipped " + i +
" bytes instead of " + entrySize + " for entry " + entry.getName());
}
assertEquals(0, zis1.skip(1));
assertEquals(0, zis1.available());
zis1.closeEntry();
assertEquals(1, zis.available());
zis1.close();
try {
zis1.available();
fail("IOException expected");
} catch (IOException ee) {
// expected
}
}
@SuppressWarnings("unchecked")
@Override
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
try
{
ZIPCompressedMessage result = new ZIPCompressedMessage();
byte[] byteArray = new byte[data.remaining()];
data.get(byteArray);
ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(byteArray));
in.getNextEntry();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] tmp = new byte[9012];
int read;
while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {
out.write(tmp, 0, read);
}
in.closeEntry();
out.flush();
in.close();
result.setMessage((Message)Serializer.readClassAndObject(ByteBuffer.wrap(out.toByteArray())));
return (T)result;
}
catch (Exception e) {
e.printStackTrace();
throw new IOException(e.toString());
}
}
@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
try
{
ZIPCompressedMessage result = new ZIPCompressedMessage();
byte[] byteArray = new byte[data.remaining()];
data.get(byteArray);
ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(byteArray));
in.getNextEntry();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] tmp = new byte[9012];
int read;
while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {
out.write(tmp, 0, read);
}
in.closeEntry();
out.flush();
in.close();
result.setMessage((Message)Serializer.readClassAndObject(ByteBuffer.wrap(out.toByteArray())));
return (T)result;
}
catch (Exception e) {
e.printStackTrace();
throw new IOException(e.toString());
}
}
@Override
protected Void doInBackground(String... params) {
try {
is = new FileInputStream(params[0] + "/" + params[1]);
zis = new ZipInputStream(new BufferedInputStream(is));
byte[] buffer = new byte[1024 * 3];
int count;
while ((ze = zis.getNextEntry()) != null) {
filename = ze.getName();
if (ze.isDirectory()) {
File fmd = new File(params[0] + "/" + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(params[0] + "/" + filename);
int total = zis.available();
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
fileCount++;
Intent zipedFiles = new Intent(AppConstants.Download.INTENT);
zipedFiles.putExtra(AppConstants.Download.FILES, context.getString(R.string.extract) + " " + fileCount);
zipedFiles.putExtra(AppConstants.Download.DOWNLOAD, AppConstants.Download.IN_EXTRACT);
LocalBroadcastManager.getInstance(context).sendBroadcast(zipedFiles);
}
File zipFile = new File(params[0] + "/" + params[1]);
zipFile.delete();
if (zipFile.getAbsolutePath().contains("tafseer")) {
copyFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + QuranApplication.getInstance()
.getString(R.string.app_folder_path) + "/tafaseer/" + params[1].replace(AppConstants.Extensions.ZIP, AppConstants.Extensions.SQLITE)),
new File(params[0] + "/" + params[1].replace(AppConstants.Extensions.ZIP, AppConstants.Extensions.SQLITE)));
}
fout.close();
zis.closeEntry();
}
//send broadcast of success or failed
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(AppConstants.Download.INTENT)
.putExtra(AppConstants.Download.DOWNLOAD, AppConstants.Download.SUCCESS ));
zis.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
@Override
protected Void doInBackground(String... params) {
try {
is = new FileInputStream(params[0] + "/" + params[1]);
zis = new ZipInputStream(new BufferedInputStream(is));
byte[] buffer = new byte[1024 * 3];
int count;
long size;
int iIndex=0;
while ((ze = zis.getNextEntry()) != null) {
iIndex++;
size = ze.getSize();
filename = ze.getName();
if (ze.isDirectory()) {
File fmd = new File(params[0] + "/" + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(params[0] + "/" + filename);
int total = zis.available();
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
fileCount++;
Intent zipedFiles = new Intent(AppConstants.Download.INTENT);
if(iIndex==1) {
zipedFiles.putExtra(AppConstants.Download.FILES, context.getString(R.string.extract) + " " + fout.getChannel().size()
+ " ( " + (int) (fout.getChannel().size() * 100) / ze.getSize() + "%)");
}else{
zipedFiles.putExtra(AppConstants.Download.FILES, context.getString(R.string.extract) + " " + iIndex+" off "+"607"
+ " ( " + (int) (iIndex * 100 )/607 + "%)");
}
zipedFiles.putExtra(AppConstants.Download.TYPE , downloadType);
zipedFiles.putExtra(AppConstants.Download.DOWNLOAD, AppConstants.Download.IN_EXTRACT);
LocalBroadcastManager.getInstance(context).sendBroadcast(zipedFiles);
}
File zipFile = new File(params[0] + "/" + params[1]);
zipFile.delete();
if (zipFile.getAbsolutePath().contains("tafseer")) {
copyFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + QuranApplication.getInstance()
.getString(R.string.app_folder_path) + "/tafaseer/" + params[1].replace(AppConstants.Extensions.ZIP, AppConstants.Extensions.SQLITE)),
new File(params[0] + "/" + params[1].replace(AppConstants.Extensions.ZIP, AppConstants.Extensions.SQLITE)));
}
fout.close();
zis.closeEntry();
}
//send broadcast of success or failed
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(AppConstants.Download.INTENT)
.putExtra(AppConstants.Download.DOWNLOAD, AppConstants.Download.SUCCESS ));
zis.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
/**
* Use the given stream to access a remote zip file
* @param inStream stream to use to access file
*/
public void openStream(InputStream inStream)
{
try
{
ZipInputStream zis = new ZipInputStream(inStream);
boolean xmlFound = false;
while (!xmlFound && zis.available() > 0)
{
ZipEntry entry = zis.getNextEntry();
String entryName = entry.toString();
if (entryName != null && entryName.length() > 4)
{
String suffix = entryName.substring(entryName.length()-4).toLowerCase();
if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
{
_xmlLoader.reset();
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.parse(zis, _xmlLoader);
XmlHandler handler = _xmlLoader.getHandler();
if (handler == null) {
_app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
}
else
{
// Send back to app
_app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES),
handler.getTrackNameList());
xmlFound = true;
}
}
}
}
// Check whether there was an xml file inside
if (!xmlFound) {
_app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
}
}
catch (Exception e) {
System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage());
}
}