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     ,           ,                                。

좋은 웹페이지 즐겨찾기