2016-10-03 6 views
0

GZIPOutputStreamを使用してtar.Gzipファイルを作成していますが、圧縮中に例外が検出された場合は別のロジックを追加しましたファイル、私のコードは3回再試行されます。私は私の再試行ロジック、それは以下の例外投げテストするIOException投げていた場合java.io.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'は、圧縮されるファイルのサイズが正しくない場合にスローされます。私のコードgetOriginalSize()

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

元のサイズを変更し、元のサイズになった再試行中のSO終わりに再び更新取得され、それは、この例外を投げていたので、今すぐ圧縮ファイルのサイズです。

関連する問題