2016-10-03 8 views
0

GZIPOutputStream을 사용하여 tar.Gzip 파일을 만들고 있는데 다른 논리를 추가했습니다. 파일을 만든 다음 코드를 세 번 다시 시도합니다. java.io.IOException : ''바이트가 ''바이트의 크기를 초과합니다. ''바이트를 입력합니다. ''

나는 그것이 아래 예외를 던지고 내 재시도 논리를 테스트하는 IOException을 던지고 때 :
java.io.IOException: request to write '4096' bytes exceeds size in header of '2644' bytes for entry 'Alldbtypes'

나는 점점 오전 예외를 줄 : org.apache.commons.io.IOUtils.copyLarge(inputStream, tarStream);

private class CompressionStream extends GZIPOutputStream { 
    // Use compression levels from the deflator class 
    public CompressionStream(OutputStream out, int compressionLevel) throws IOException { 
     super(out); 
     def.setLevel(compressionLevel); 
    } 
} 

public void createTAR(){ 
    boolean isSuccessful=false; 
    int count = 0; 
    int maxTries = 3; 
    while(!isSuccessful) { 
     InputStream inputStream =null; 
     FileOutputStream outputStream =null; 
     CompressionStream compressionStream=null; 
     OutputStream md5OutputStream = null; 
     TarArchiveOutputStream tarStream = null; 
     try{ 
      inputStream = new BufferedInputStream(new FileInputStream(rawfile)); 
      File stagingPath = new File("C:\\Workarea\\6d22b6a3-564f-42b4-be83-9e1573a718cd\\b88beb62-aa65-4ad5-b46c-4f2e9c892259.tar.gz"); 
      boolean isDeleted = false; 
      if(stagingPath.exists()){ 
       isDeleted = stagingPath.delete(); 
       if(stagingPath.exists()){ 
        try { 
         FileUtils.forceDelete(stagingPath); 
        }catch (IOException ex){ 
         //ignore 
        } 
       } 
      } 
      outputStream = new FileOutputStream(stagingPath); 
      if (isCompressionEnabled) { 
       compressionStream = new 
         CompressionStream(outputStream, getCompressionLevel(om)); 
      } 
      final MessageDigest outputDigest = MessageDigest.getInstance("MD5"); 
      md5OutputStream = new DigestOutputStream(isCompressionEnabled ? compressionStream : outputStream, outputDigest); 
      tarStream = new TarArchiveOutputStream(new BufferedOutputStream(md5OutputStream)); 
      tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); 
      tarStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); 
      TarArchiveEntry entry = new TarArchiveEntry("Alldbtypes"); 
      entry.setSize(getOriginalSize()); 
      entry.setModTime(getLastModified().getMillis()); 
      tarStream.putArchiveEntry(entry); 
      org.apache.commons.io.IOUtils.copyLarge(inputStream, tarStream); 
      inputStream.close(); 
      tarStream.closeArchiveEntry(); 
      tarStream.finish(); 
      tarStream.close(); 
      String digest = Hex.encodeHexString(outputDigest.digest()); 
      setChecksum(digest); 
      setIngested(DateTime.now()); 
      setOriginalSize(FileUtils.sizeOf(stagingPath)); 
      isSuccessful =true; 
     } catch (IOException e) { 
      if (++count == maxTries) { 
       throw new RuntimeException("Exception: " + e.getMessage(), e); 
      } 
     } catch (NoSuchAlgorithmException e) { 
      throw new RuntimeException(Exception("MD5 hash algo not installed."); 
     } catch (Exception e) { 
      throw new RuntimeException("Exception: " + e.getMessage(), e); 
     } finally { 
      org.apache.commons.io.IOUtils.closeQuietly(inputStream); 
      try { 
       tarStream.flush(); 
       tarStream.finish(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      org.apache.commons.io.IOUtils.closeQuietly(tarStream); 
      org.apache.commons.io.IOUtils.closeQuietly(compressionStream); 
      org.apache.commons.io.IOUtils.closeQuietly(md5OutputStream); 
      org.apache.commons.io.IOUtils.closeQuietly(outputStream); 
     } 

    } 
} 

답변

2

케이스가 해결. 이 예외 java.io.IOException: request to write '4096' bytes exceeds size in header of '2644' bytes for entry 'Alldbtypes'은 압축 될 파일의 ​​크기가 올바르지 않을 때 throw됩니다. 재시도 원래 크기가 변경되었고, 그것은이 예외를 던지는 그래서 원래 크기로 지금은 압축 파일의 크기 그래서 내 코드 getOriginalSize()에서

TarArchiveEntry entry = new TarArchiveEntry("Alldbtypes"); 
entry.setSize(getOriginalSize()); 

는 말에 다시 업데이트지고 있습니다.