修复中文AndroidZipFile不能读取非ASCII字符文件名
This commit is contained in:
@@ -39,7 +39,7 @@ public final class PfdHelper {
|
||||
|
||||
public static long length(ParcelFileDescriptor pfd) throws IOException {
|
||||
try {
|
||||
return android.system.Os.lseek(pfd.getFileDescriptor(), 0, OsConstants.SEEK_END);
|
||||
return android.system.Os.fstat(pfd.getFileDescriptor()).st_size; //android.system.Os.lseek(pfd.getFileDescriptor(), 0, OsConstants.SEEK_END);
|
||||
} catch (ErrnoException e) {
|
||||
throw rethrowAsIOException(e);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ public class AndroidZipEntry implements ZipConstants, Cloneable {
|
||||
private static Calendar cal;
|
||||
|
||||
private final String name;
|
||||
private final int nameLen;
|
||||
private int size;
|
||||
private int compressedSize;
|
||||
private int crc;
|
||||
@@ -54,13 +55,15 @@ public class AndroidZipEntry implements ZipConstants, Cloneable {
|
||||
* @throws NullPointerException when name is null.
|
||||
* @throws IllegalArgumentException when name is bigger then 65535 chars.
|
||||
*/
|
||||
public AndroidZipEntry(String name) {
|
||||
int length = name.length();
|
||||
if (length > 65535)
|
||||
throw new IllegalArgumentException("name length is " + length);
|
||||
public AndroidZipEntry(String name, int nameLen) {
|
||||
//int length = name.length();
|
||||
this.nameLen = nameLen;
|
||||
if (nameLen > 65535)
|
||||
throw new IllegalArgumentException("name length is " + nameLen);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of the given zip entry.
|
||||
*
|
||||
@@ -68,6 +71,7 @@ public class AndroidZipEntry implements ZipConstants, Cloneable {
|
||||
*/
|
||||
public AndroidZipEntry(AndroidZipEntry e) {
|
||||
name = e.name;
|
||||
nameLen = e.nameLen;
|
||||
known = e.known;
|
||||
size = e.size;
|
||||
compressedSize = e.compressedSize;
|
||||
@@ -116,6 +120,10 @@ public class AndroidZipEntry implements ZipConstants, Cloneable {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getNameLen() {
|
||||
return nameLen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time of last modification of the entry.
|
||||
*
|
||||
|
||||
@@ -242,9 +242,9 @@ public class AndroidZipFile implements ZipConstants {
|
||||
buffer = new byte[needBuffer];
|
||||
|
||||
PfdHelper.readFully(pfd, buffer, 0, nameLen);
|
||||
String name = new String(buffer, 0, 0, nameLen);
|
||||
String name = new String(buffer, 0, nameLen);
|
||||
|
||||
AndroidZipEntry entry = new AndroidZipEntry(name);
|
||||
AndroidZipEntry entry = new AndroidZipEntry(name, nameLen);
|
||||
entry.setMethod(method);
|
||||
entry.setCrc(crc & 0xffffffffL);
|
||||
entry.setSize(size & 0xffffffffL);
|
||||
@@ -361,10 +361,10 @@ public class AndroidZipFile implements ZipConstants {
|
||||
if (entry.getMethod() != readLeShort(locBuf, LOCHOW))
|
||||
throw new ZipException("Compression method mismatch: " + name);
|
||||
|
||||
if (entry.getName().length() != readLeShort(locBuf, LOCNAM))
|
||||
if (entry.getNameLen() != readLeShort(locBuf, LOCNAM))
|
||||
throw new ZipException("file name length mismatch: " + name);
|
||||
|
||||
int extraLen = entry.getName().length() + readLeShort(locBuf, LOCEXT);
|
||||
int extraLen = entry.getNameLen() + readLeShort(locBuf, LOCEXT);
|
||||
return entry.offset + LOCHDR + extraLen;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user