if(compressed == null)
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(new ByteArrayInputStream(compressed)); // ZipEntry entry = zin.getNextEntry();
// 50K
byte[] buffer = new byte[1024];
int offset = 0;
System.out.println("getnext=="+zin.getNextEntry());
System.out.println("offset="+zin.read(buffer));
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if(zin != null) {
try {zin.close();} catch(IOException e) {}
}
if(in != null) {
try {in.close();} catch(IOException e) {}
}
if(out != null) {
try {out.close();} catch(IOException e) {}
}
}
return decompressed;
}
为什么我的zin.getNextEntry()=null,offset=-1???
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(new ByteArrayInputStream(compressed)); // ZipEntry entry = zin.getNextEntry();
// 50K
byte[] buffer = new byte[1024];
int offset = 0;
System.out.println("getnext=="+zin.getNextEntry());
System.out.println("offset="+zin.read(buffer));
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if(zin != null) {
try {zin.close();} catch(IOException e) {}
}
if(in != null) {
try {in.close();} catch(IOException e) {}
}
if(out != null) {
try {out.close();} catch(IOException e) {}
}
}
return decompressed;
}
为什么我的zin.getNextEntry()=null,offset=-1???
public static File getFileFromZipByName(File zip, String path, String findName) {
File file = null; try {
ZipFile zipFile = new ZipFile(zip);
Enumeration enu = zipFile.getEntries(); while(enu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enu.nextElement();
if(findName.equals(entry.getName())) {
file = new File(path + findName);
InputStream in = zipFile.getInputStream(entry);
FileOutputStream out = new FileOutputStream(file);
int len = 0;
byte[] bytes = new byte[256]; while((len = in.read(bytes, 0, bytes.length)) != -1) {
out.write(bytes, 0, len);
} out.flush();
out.close();
in.close();
}
}
}
catch(Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
} return file;
}
自己改下。