2015-04-14 55 views
0

我正在使用TrueZip進行壓縮。這裏是我的代碼是什麼樣子TrueZip壓縮佔用太多時間

public String compress() throws IOException { 
    if (logLocations.isEmpty()) { 
     throw new IllegalStateException("no logs provided to compress"); 
    } 

    removeDestinationIfExists(desiredArchive); 

    final TFile destinationArchive = new TFile(desiredArchive + "/diagnostics"); 
    for (final String logLocation : logLocations) { 
     final TFile log = new TFile(logLocation); 
     if (!log.exists()) { 
     LOGGER.debug("{} does not exist, ignoring."); 
     continue; 
     } 
     if (log.isDirectory()) { 
     log.cp_r(destinationArchive); 
     } else { 
     final String newLogLocation = 
      new TFile(destinationArchive.getAbsolutePath()) + SLASH + 
      getLogNameFromPath(logLocation); 
     log.cp(new TFile(newLogLocation)); 
     } 
    } 
    return destinationArchive.getEnclArchive().getAbsolutePath(); 
    } 

和我的測試

@Test 
    public void testBenchMarkWithHprof() throws IOException { 
    final FileWriter logLocations; 
    String logLocationPath = "/Users/harit/Downloads/tmp/logLocations.txt"; 
    { 
     logLocations = new FileWriter(logLocationPath); 
     logLocations.write("Test3"); 
     logLocations.write("\n"); 
     logLocations.close(); 
    } 
    final LPLogCompressor compressor = new LPLogCompressor("/Users/harit/Downloads/tmp", 
                  new File(logLocationPath), 
                  "/Users/harit/Downloads/tmp/TestOut"); 
    final long startTime = System.currentTimeMillis(); 
    compressor.compress(); 
    System.out.println("Time taken (msec): " + (System.currentTimeMillis() - startTime)); 
    } 

和我的數據目錄Test3看起來像

Test3/ 
     java_pid1748.hprof 

的文件大小爲2.83GB 當我運行測試,它接管了22分鐘
然而,當我使用壓縮的Native OSX compress (right click -> compress)相同的文件,只需要2分鐘

爲什麼有這麼大的區別呢?

感謝

UPDATE

基於@Satnam的建議,我連着一個調試器,看看怎麼回事,這是我找到

enter image description here enter image description here

無的TrueZip線程正在運行?真?道歉我第一次使用探查器

+0

爲什麼不使用探查? – satnam

+0

是否存在差異 - 文件屬性,如所有者和只讀存儲? (可在小目錄上測試)。使用標準的java意味着可以創建一個zip文件系統,只需使用'Files.copy'複製一個操作即可將磁盤中的目錄複製到zip中。 –

+0

@satnam,我更新了快照,你看到差異了嗎? – daydreamer

回答

1

原因在這種情況下使用默認Deflater這是Deflater.BEST_COMPRESSION

我重寫ZipDriver類在水平

import de.schlichtherle.truezip.fs.archive.zip.ZipDriver; 
import de.schlichtherle.truezip.socket.IOPoolProvider; 

import java.util.zip.Deflater; 

public class OverrideZipDriver extends ZipDriver { 

    public OverrideZipDriver(final IOPoolProvider ioPoolProvider) { 
    super(ioPoolProvider); 
    } 

    @Override 
    public int getLevel() { 
    return Deflater.DEFAULT_COMPRESSION; 
    } 
} 

,然後在我的Compressor類,我做

public LPLogCompressor(final String logProcessorInstallPath, final File logLocationsSource, 
         final String desiredArchive) throws IOException { 
    this.desiredArchive = desiredArchive + DOT + getDateTimeStampFormat() + ZIP; 
    logLocations = getLogLocations(logProcessorInstallPath, logLocationsSource); 
    enableLogCompression(); 
    } 

    private static void enableLogCompression() { 
    TConfig.get().setArchiveDetector(
     new TArchiveDetector(TArchiveDetector.NULL, new Object[][]{ 
      {"zip", new OverrideZipDriver(IOPoolLocator.SINGLETON)},})); 
    TConfig.push(); 
    } 

可以讀取線程here

+0

在執行推送之前,您正在修改(可能是全局的)配置。請在這裏查看正確的用法:https://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TConfig.html#local –