Coding/Java

Java에서 zip압축의 내용을 String으로 추출하게

realizm 2013. 10. 2. 11:34


zip으로 압축되서 DB에 저장된 데이터가 있는데..


운영할때 실 데이터를 확인하기 위한 툴이 필요해 작성한 코드입니다.



	public HashMap<String, String> unzip(byte[] baZip){
		
		final int BUFFER_SIZE = 1024 * 4; 
		
		ByteArrayInputStream bais = new ByteArrayInputStream(baZip);
		ZipInputStream zis = new ZipInputStream(bais);
		ZipEntry ze = null;
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		byte[] buf = new byte[ BUFFER_SIZE ];
		
		try {
			
			while( (ze = zis.getNextEntry()) != null ) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				
				int len = 0;
				while( (len = zis.read(buf, 0, BUFFER_SIZE) ) != -1 ){
					baos.write(buf, 0, len);
				}
				
				map.put(ze.getName(), baos.toString()); //k:FileName , v:Content
				baos.flush();
				baos.close();
				zis.closeEntry();
			}
			

			zis.close();
			bais.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return map;
	}

다 짜놓고 나니.. 


그냥 FileOutputStream으로 뽑느거랑 아무런 차이가 없네요;;


인터넷에서 찾아본 일부 코드가...

while( (len = zis.read(buf) != -1 ){
	baos.write(buf);
}

이렇게 되어있었는데.. 

제가 테스트한 소스에서는 length가 없으면 비정상 동작하더군요.