0

Production code (example)

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

Somehow I want to test my class without using complex external zip resource (nobody wants that). Is there some simple solution to do something like this?

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

I want to just "cheat" class that InputStream is a zip file without going into content.

Dawid Pura
  • 991
  • 9
  • 32

1 Answers1

0

copied 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();
        }
    }
}
Community
  • 1
  • 1
Danielson
  • 2,605
  • 2
  • 28
  • 51
  • And that it is what i meant about "simplicity". Anyway, that is a correct answer, thanks! – Dawid Pura Jul 28 '15 at 07:15
  • You can use a subselection, I just copied what I use... When I found it, I saw I looked for it before, and copied the code from the link, as stated at the top... Works great.. – Danielson Jul 28 '15 at 07:17