2015-07-28 47 views
0

生產代碼(例如)如何在輸入流中模擬zip?

class ClassToTest { 
    public SomeResult interpreteZipFile(InputStream is) { 
    ZippedInputStream zis = new ZippedInputStream(is); 
    //other code with something returned 
    } 
} 

不知怎的,我想測試我的課,而無需使用複雜的外部拉鍊資源(大家都不希望發生)。有沒有一些簡單的解決方案來做這樣的事情?

classTotest.interpreteZipFile(new MockToBeZippedFile(new ReaderInputStream(new StringReader("some content")))); 

我想就「騙」類InputStream是沒有進入內容的zip文件。

+0

你的意思是 「模擬輸入流」? 「簡單」的ZipInputStream不夠好嗎? – Danielson

+0

'ZipInputStream'正在從文件解壓縮/讀取。不要壓縮它。我在生產代碼中解壓縮流,所以我需要在測試中壓縮它。 –

+0

足夠公平,'ZipOutputStream'然後 – Danielson

回答

0

複製java.util.zip - Recreating directory structure

import java.io.Closeable; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URI; 
import java.util.Deque; 
import java.util.LinkedList; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 

public class ZipIt { 

    public void zipDir(String folderToZip, String zipName) { 
     File directory = new File(folderToZip); 
     File zipFile = new File(zipName); 
     try { 
      ZipIt.zip(directory, zipFile); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void zipDir(String folderToZip, String toLocation, String zipName) { 
     this.zipDir(folderToZip, toLocation + File.separator + zipName); 
    } 

    private static void zip(File directory, File zipfile) throws IOException { 
     URI base = directory.toURI(); 
     Deque<File> queue = new LinkedList<File>(); 
     queue.push(directory); 
     OutputStream out = new FileOutputStream(zipfile); 
     Closeable res = out; 
     try { 
      ZipOutputStream zout = new ZipOutputStream(out); 
      res = zout; 
      while (!queue.isEmpty()) { 
       directory = queue.pop(); 
       for (File kid : directory.listFiles()) { 
        String name = base.relativize(kid.toURI()).getPath(); 
        if (kid.isDirectory()) { 
         queue.push(kid); 
         name = name.endsWith("/") ? name : name + "/"; 
         zout.putNextEntry(new ZipEntry(name)); 
        } else { 
         zout.putNextEntry(new ZipEntry(name)); 
         copy(kid, zout); 
         zout.closeEntry(); 
        } 
       } 
      } 
     } finally { 
      res.close(); 
     } 
    } 

    private static void copy(InputStream in, OutputStream out) 
      throws IOException { 
     byte[] buffer = new byte[1024]; 
     while (true) { 
      int readCount = in.read(buffer); 
      if (readCount < 0) { 
       break; 
      } 
      out.write(buffer, 0, readCount); 
     } 
    } 

    private static void copy(File file, OutputStream out) throws IOException { 
     InputStream in = new FileInputStream(file); 
     try { 
      copy(in, out); 
     } finally { 
      in.close(); 
     } 
    } 
} 
+0

這就是我的意思是「簡單」。無論如何,這是一個正確的答案,謝謝! –

+0

你可以使用子選擇,我只是複製了我使用的東西......當我找到它時,我看到我之前找過它,並從鏈接複製代碼,如頂部所述......很好。 – Danielson