ZipInputStream 문제
4722 단어 어지럽다ZipInputStream
private static String getUTF8String(byte[] b, int off, int len) {
// First, count the number of characters in the sequence
int count = 0;
int max = off + len;
int i = off;
while (i < max) {
int c = b[i++] & 0xff;
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
count++;
break;
case 12: case 13:
// 110xxxxx 10xxxxxx
if ((int)(b[i++] & 0xc0) != 0x80) {
throw new IllegalArgumentException();
}
count++;
break;
case 14:
// 1110xxxx 10xxxxxx 10xxxxxx
if (((int)(b[i++] & 0xc0) != 0x80) ||
((int)(b[i++] & 0xc0) != 0x80)) {
throw new IllegalArgumentException();
}
count++;
break;
default:
// 10xxxxxx, 1111xxxx
throw new IllegalArgumentException();
}
}
if (i != max) {
throw new IllegalArgumentException();
}
// Now decode the characters...
char[] cs = new char[count];
i = 0;
while (off < max) {
int c = b[off++] & 0xff;
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
cs[i++] = (char)c;
break;
case 12: case 13:
// 110xxxxx 10xxxxxx
cs[i++] = (char)(((c & 0x1f) << 6) | (b[off++] & 0x3f));
break;
case 14:
// 1110xxxx 10xxxxxx 10xxxxxx
int t = (b[off++] & 0x3f) << 6;
cs[i++] = (char)(((c & 0x0f) << 12) | t | (b[off++] & 0x3f));
break;
default:
// 10xxxxxx, 1111xxxx
throw new IllegalArgumentException();
}
}
return new String(cs, 0, count);
}
이상은 ZipInputStream이 중국어 항목이 있는 zip 패키지를 읽을 때 이상을 던지는 방법입니다. 이 방법은 두 개의while 순환이 있습니다. 첫 번째는 통계 항목의 이름 부호count이고, 두 번째는 UTF-8 알고리즘으로byte 수조를 디코딩합니다. 문자열을 추출하면 UTF-8이 아닌 모든 문자열이 이상을 던집니다.중국어 시스템의 기본 인코딩은 GBK이기 때문에 항목 이름이 중국어를 포함할 때 바로 이상을 던집니다.
인코딩이 순환 통계를 권장하는지'변장수조'를 권장하는지 모르겠다. 이 방법은 다음과 같이 바꿀 수 있다.
private static String getUTF8String(byte[] b, int off, int len) {CharArrayWriter caw=new CharArrayWriter();
int i = 0;
while (off < max) {
int c = b[off++] & 0xff;
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
caw.append((char) c);
break;
case 12:
case 13:
// 110xxxxx 10xxxxxx
//
caw.append((char) (((c & 0x1f) << 6) | (b[off++] & 0x3f)));
break;
case 14:
// 1110xxxx 10xxxxxx 10xxxxxx
int t = (b[off++] & 0x3f) << 6;
//
caw.append((char) (((c & 0x0f) << 12) | t | (b[off++] & 0x3f)));
break;
default:
// 10xxxxxx, 1111xxxx
throw new IllegalArgumentException();
}
}
char[] ch=caw.toCharArray();
return new String(ch, 0, ch.length);
}
CharArrayWriter , “ ”, 32 , 。 。
。 String byte , , ? :
private static String getUTF8String(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
{
try{
return new String(paramArrayOfByte, paramInt1, paramInt2,"UTF-8");
}catch(UnsupportedEncodingException e){
e.printStackTrace();
return "encoding error!";
}
}
, 。
, , :
private static String filenameEncoding=Charset.defaultCharset().toString();//
public static void setFilenameEncoding(String encoding){
filenameEncoding=encoding;
}
private static String getUTF8String(byte[] b, int off, int len) {
{
try{
return new String(paramArrayOfByte, paramInt1, paramInt2,filenameEncoding);
}catch(UnsupportedEncodingException e){
e.printStackTrace();
return "encoding error!";
}
}
“ ” 。
: SUN , , 。
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SceneBuilder11의 대미지 수정JavaFX 어플리케이션을 제작할 때 편리한 SceneBuilder 하지만 부터 Windows Installer에 SceneBuilder 11.00을 설치하면 일본어 메뉴 등 난자 문제가 발생할 수 있습니다. 는 매...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.