Search in sources :

Example 26 with FilterInputStream

use of java.io.FilterInputStream in project poi by apache.

the class TestHwmfParsing method fetchWmfFromGovdocs.

@Test
@Ignore("This is work-in-progress and not a real unit test ...")
public void fetchWmfFromGovdocs() throws IOException {
    URL url = new URL("http://digitalcorpora.org/corpora/files/govdocs1/by_type/ppt.zip");
    File outdir = new File("build/ppt");
    outdir.mkdirs();
    ZipInputStream zis = new ZipInputStream(url.openStream());
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        String basename = ze.getName().replaceAll(".*?([^/]+)\\.wmf", "$1");
        FilterInputStream fis = new FilterInputStream(zis) {

            @Override
            public void close() throws IOException {
            }
        };
        try {
            SlideShow<?, ?> ss = SlideShowFactory.create(fis);
            int wmfIdx = 1;
            for (PictureData pd : ss.getPictureData()) {
                if (pd.getType() != PictureType.WMF)
                    continue;
                byte[] wmfData = pd.getData();
                String filename = String.format(Locale.ROOT, "%s-%04d.wmf", basename, wmfIdx);
                FileOutputStream fos = new FileOutputStream(new File(outdir, filename));
                fos.write(wmfData);
                fos.close();
                wmfIdx++;
            }
            ss.close();
        } catch (Exception e) {
            System.out.println(ze.getName() + " ignored.");
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) PictureData(org.apache.poi.sl.usermodel.PictureData) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

FilterInputStream (java.io.FilterInputStream)26 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileOutputStream (java.io.FileOutputStream)4 URL (java.net.URL)4 ZipEntry (java.util.zip.ZipEntry)4 BufferedOutputStream (java.io.BufferedOutputStream)2 DataInputStream (java.io.DataInputStream)2 OutputStream (java.io.OutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 Random (java.util.Random)2 JarFile (java.util.jar.JarFile)2 JarInputStream (java.util.jar.JarInputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ZipInputStream (java.util.zip.ZipInputStream)2