Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read(byte[] b, int off, int len) throws IOException 

Source Link

Document

Reads from the current ZIP entry into an array of bytes.

Usage

From source file:jp.co.opentone.bsol.linkbinder.service.correspon.impl.CorresponSearchServiceImplTest.java

/**
 * ZIP?./*from  w w w . j  av  a 2 s.c o  m*/
 */
@Test
public void testGenerateZip() throws Exception {
    MockAbstractService.RET_CURRENT_PROJECT_ID = "PJ1";

    //  ?????
    //  ???????????
    //  ????
    Correspon noNotAssigned = new Correspon();
    PropertyUtils.copyProperties(noNotAssigned, list.get(list.size() - 1));
    noNotAssigned.setCorresponNo(null);
    noNotAssigned.setId(Long.valueOf(list.size() + 1));

    list.add(noNotAssigned);

    Correspon c = new Correspon();
    c.setId(7L);
    c.setCorresponNo("YOC:OT:BUILDING-00007");
    CorresponGroup from = new CorresponGroup();
    from.setId(new Long(6L));
    from.setName("YOC:BUILDING");
    c.setFromCorresponGroup(from);
    c.setSubject("Mock");
    List<AddressCorresponGroup> addressGroups = new ArrayList<AddressCorresponGroup>();
    AddressCorresponGroup addressGroup = new AddressCorresponGroup();
    CorresponGroup group = new CorresponGroup();
    group.setId(2L);
    group.setName("YOC:IT");
    addressGroup.setCorresponGroup(group);
    addressGroup.setAddressType(AddressType.TO);
    addressGroups.add(addressGroup);
    addressGroup = new AddressCorresponGroup();
    group = new CorresponGroup();
    group.setId(2L);
    group.setName("TOK:BUILDING");
    addressGroup.setCorresponGroup(group);
    addressGroup.setAddressType(AddressType.CC);
    addressGroups.add(addressGroup);
    c.setAddressCorresponGroups(addressGroups);
    CorresponType ct = new CorresponType();
    ct.setId(new Long(1L));
    ct.setCorresponType("Request");
    c.setCorresponType(ct);
    c.setWorkflowStatus(WorkflowStatus.ISSUED);
    c.setIssuedAt(new GregorianCalendar(2009, 3, 5, 1, 1, 1).getTime());
    c.setCreatedAt(new GregorianCalendar(2009, 3, 1, 1, 1, 1).getTime());
    c.setUpdatedAt(new GregorianCalendar(2009, 3, 10, 10, 10, 10).getTime());
    User u = new User();
    u.setEmpNo("00001");
    u.setNameE("Test User");
    c.setIssuedBy(u);
    c.setCreatedBy(u);
    c.setUpdatedBy(u);
    c.setCorresponStatus(CorresponStatus.OPEN);
    c.setCustomField1Label("FIELD1");
    c.setCustomField1Value("VALUE1");
    c.setCustomField2Label("FIELD2");
    c.setCustomField2Value("VALUE2");
    c.setCustomField3Label("FIELD3");
    c.setCustomField3Value("VALUE3");
    c.setCustomField4Label("FIELD4");
    c.setCustomField4Value("VALUE4");
    c.setCustomField5Label("FIELD5");
    c.setCustomField5Value("VALUE5");
    c.setCustomField6Label("FIELD6");
    c.setCustomField6Value("VALUE6");
    c.setCustomField7Label("FIELD7");
    c.setCustomField7Value("VALUE7");
    c.setCustomField8Label("FIELD8");
    c.setCustomField8Value("VALUE8");
    c.setCustomField9Label("FIELD9");
    c.setCustomField9Value("VALUE9");
    c.setCustomField10Label("FIELD10");
    c.setCustomField10Value("VALUE10");
    c.setWorkflows(new ArrayList<Workflow>());
    c.setPreviousRevCorresponId(null);
    group = new CorresponGroup();
    group.setName("YOC:IT");
    c.setToCorresponGroup(group);
    c.setToCorresponGroupCount(2L);
    c.setIssuedAt(null);
    c.setIssuedBy(null);
    c.setReplyRequired(null);
    c.setDeadlineForReply(null);
    list.add(c);

    MockCorresponService.RET_FIND = list;
    MockAbstractService.CORRESPONS = list;
    byte[] actual = service.generateZip(list);

    ByteArrayInputStream bi = new ByteArrayInputStream(actual);
    ZipInputStream zis = new ZipInputStream(bi);
    byte[] b = new byte[1024];
    StringBuilder sb = new StringBuilder();
    String[] expFileNames = { "PJ1_YOC-OT-BUILDING-00001(0000000001).html",
            "PJ1_YOC-OT-BUILDING-00002(0000000002).html", "PJ1_YOC-OT-BUILDING-00003(0000000003).html",
            "PJ1_YOC-OT-BUILDING-00001-001(0000000004).html", "PJ1_YOC-OT-BUILDING-00001-002(0000000005).html",
            "PJ1_(0000000006).html", "PJ1_YOC-OT-BUILDING-00007(0000000007).html" };
    int index = 0;
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
            String expFileName = expFileNames[index];
            assertEquals(expFileName, ze.getName());
            if (ze.isDirectory()) {
                continue;
            }

            c = list.get(index);
            int i;
            while ((i = zis.read(b, 0, b.length)) != -1) {
                sb.append(new String(b, 0, i));
            }
            String html = sb.toString();

            // ??script????????
            assertTrue(html.contains("<body>"));
            assertFalse(html.contains(">Print</a>"));
            assertFalse(html.contains(">Close</a>"));

            // ???
            assertTrue(c.getId().toString(), html.contains(getExpectedString(c.getId().toString())));
            assertTrue(c.getCorresponStatus().getLabel(),
                    html.contains(getExpectedHTMLString(c.getCorresponStatus().getLabel())));

            //  ?????
            //  ?????????
            if (c.getCorresponNo() == null) {
                assertTrue(c.getCorresponNo(),
                        html.contains(getExpectedHTMLString(CorresponPageFormatter.DEFAULT_CORRESPON_NO)));
            } else {
                assertTrue(c.getCorresponNo(), html.contains(getExpectedHTMLString(c.getCorresponNo())));
            }

            assertTrue(c.getWorkflowStatus().getLabel(),
                    html.contains(getExpectedHTMLString(c.getWorkflowStatus().getLabel())));
            if (c.getParentCorresponId() != null) {
                assertTrue(c.getParentCorresponNo(),
                        html.contains(getExpectedHTMLString(c.getParentCorresponNo())));
            }
            if (c.getPreviousRevCorresponId() != null) {
                assertTrue(c.getPreviousRevCorresponNo(),
                        html.contains(getExpectedHTMLString(c.getPreviousRevCorresponNo())));
            }
            assertTrue(c.getFromCorresponGroup().getName() + " " + index,
                    html.contains(getExpectedString(c.getFromCorresponGroup().getName())));
            if (c.getAddressCorresponGroups() != null) {
                for (AddressCorresponGroup addressCorresponGroup : c.getAddressCorresponGroups()) {
                    assertTrue(addressCorresponGroup.getCorresponGroup().getName(),
                            html.contains(addressCorresponGroup.getCorresponGroup().getName()));
                    if (addressCorresponGroup.getUsers() == null) {
                        continue;
                    }
                    for (AddressUser aUser : addressCorresponGroup.getUsers()) {
                        if (addressCorresponGroup.isTo() && aUser.isAttention()) {
                            assertTrue(aUser.getUser().getLabel(), html.contains(aUser.getUser().getLabel()));
                            if (aUser.getPersonInCharges() != null) {
                                assertTrue(aUser.getPersonInCharges().get(0).getUser().getLabel(),
                                        html.contains(aUser.getPersonInCharges().get(0).getUser().getLabel()));
                            }
                        } else if (addressCorresponGroup.isTo() && !aUser.isAttention()) {
                            assertFalse(aUser.getUser().getLabel(), html.contains(aUser.getUser().getLabel()));
                        } else if (addressCorresponGroup.isCc() && aUser.isCc()) {
                            assertTrue(aUser.getUser().getLabel(), html.contains(aUser.getUser().getLabel()));
                        }
                    }
                }
            }
            assertTrue(c.getCorresponType().getName(),
                    html.contains(getExpectedHTMLString(c.getCorresponType().getName())));
            assertTrue(c.getSubject(), html.contains(getExpectedHTMLString(c.getSubject())));
            assertTrue(c.getBody(), html.contains(getExpectedHTMLString(c.getBody())));
            if (ReplyRequired.YES.equals(c.getReplyRequired())) {
                assertTrue(c.getDeadlineForReply().toString(),
                        html.contains(f.format(c.getDeadlineForReply())));
            }
            if (c.getFile1FileName() != null) {
                assertTrue(c.getFile1FileName(), html.contains(c.getFile1FileName()));
            }
            if (c.getFile2FileName() != null) {
                assertTrue(c.getFile2FileName(), html.contains(c.getFile2FileName()));
            }
            if (c.getFile3FileName() != null) {
                assertTrue(c.getFile3FileName(), html.contains(c.getFile3FileName()));
            }
            if (c.getFile4FileName() != null) {
                assertTrue(c.getFile4FileName(), html.contains(c.getFile4FileName()));
            }
            if (c.getFile5FileName() != null) {
                assertTrue(c.getFile5FileName(), html.contains(c.getFile5FileName()));
            }
            if (c.getCustomField1Label() != null) {
                assertTrue(c.getCustomField1Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField1Label())));
                assertTrue(c.getCustomField1Value(), html.contains(c.getCustomField1Value()));
            }
            if (c.getCustomField2Label() != null) {
                assertTrue(c.getCustomField2Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField2Label())));
                assertTrue(c.getCustomField2Value(), html.contains(c.getCustomField2Value()));
            }
            if (c.getCustomField3Label() != null) {
                assertTrue(c.getCustomField3Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField3Label())));
                assertTrue(c.getCustomField3Value(), html.contains(c.getCustomField3Value()));
            }
            if (c.getCustomField4Label() != null) {
                assertTrue(c.getCustomField4Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField4Label())));
                assertTrue(c.getCustomField4Value(), html.contains(c.getCustomField4Value()));
            }
            if (c.getCustomField5Label() != null) {
                assertTrue(c.getCustomField5Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField5Label())));
                assertTrue(c.getCustomField5Value(), html.contains(c.getCustomField5Value()));
            }
            if (c.getCustomField6Label() != null) {
                assertTrue(c.getCustomField6Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField6Label())));
                assertTrue(c.getCustomField6Value(), html.contains(c.getCustomField6Value()));
            }
            if (c.getCustomField7Label() != null) {
                //  ???HTML??????????...
                //  ????????????????
                //  ?
                //                    assertTrue("" + index + "*"+c.getCustomField7Label()+"*",
                //                        html.contains(getExpectedHTMLString(c.getCustomField7Label())));
                //                    assertTrue(c.getCustomField7Value(),
                //                        html.contains(c.getCustomField7Value()));
            }
            if (c.getCustomField8Label() != null) {
                assertTrue(c.getCustomField8Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField8Label())));
                assertTrue(c.getCustomField8Value(), html.contains(c.getCustomField8Value()));
            }
            if (c.getCustomField9Label() != null) {
                assertTrue(c.getCustomField9Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField9Label())));
                assertTrue(c.getCustomField9Value(), html.contains(c.getCustomField9Value()));
            }
            if (c.getCustomField10Label() != null) {
                assertTrue(c.getCustomField10Label(),
                        html.contains(getExpectedHTMLString(c.getCustomField10Label())));
                assertTrue(c.getCustomField10Value(), html.contains(c.getCustomField10Value()));
            }
            //Created??????????
            //                assertTrue(c.getCreatedAt().toString(),
            //                   html.contains(getExpectedHTMLString(f2.format(c.getCreatedAt()))));
            if (c.getIssuedAt() != null) {
                assertTrue(c.getIssuedAt().toString(),
                        html.contains(getExpectedHTMLString(f2.format(c.getIssuedAt()))));
            }
            assertTrue(c.getUpdatedAt().toString(),
                    html.contains(getExpectedHTMLString(f2.format(c.getUpdatedAt()))));
            //Created??????????
            //                assertTrue(c.getCreatedBy().getNameE() + "/" + c.getCreatedBy().getEmpNo(),
            //                    html.contains(
            //                        c.getCreatedBy().getNameE() + "/" + c.getCreatedBy().getEmpNo()));
            if ((c.getIssuedBy() != null) && (c.getIssuedBy().getNameE() != null)
                    && (c.getIssuedBy().getEmpNo() != null)) {
                assertTrue(c.getIssuedBy().getNameE() + "/" + c.getIssuedBy().getEmpNo(),
                        html.contains(c.getIssuedBy().getNameE() + "/" + c.getIssuedBy().getEmpNo()));
            }
            assertTrue(c.getUpdatedBy().getNameE() + "/" + c.getUpdatedBy().getEmpNo(),
                    html.contains(c.getUpdatedBy().getNameE() + "/" + c.getUpdatedBy().getEmpNo()));
            if (c.getWorkflows() != null) {
                for (Workflow flow : c.getWorkflows()) {
                    assertTrue(flow.getWorkflowNo().toString(),
                            html.contains(getExpectedHTMLString(flow.getWorkflowNo().toString())));
                    assertTrue(flow.getWorkflowType().getLabel(),
                            html.contains(getExpectedHTMLString(flow.getWorkflowType().getLabel())));
                    assertTrue(flow.getUser().getNameE() + "/" + flow.getUser().getEmpNo(), html.contains(
                            getExpectedString(flow.getUser().getNameE() + "/" + flow.getUser().getEmpNo())));
                    assertTrue(flow.getWorkflowProcessStatus().getLabel(),
                            html.contains(getExpectedString(flow.getWorkflowProcessStatus().getLabel())));
                    if (flow.getUpdatedBy() != null) {
                        assertTrue(flow.getUpdatedBy().getNameE() + "/" + flow.getUpdatedBy().getEmpNo(),
                                html.contains(getExpectedString(flow.getUpdatedBy().getNameE() + "/"
                                        + flow.getUpdatedBy().getEmpNo())));
                        assertTrue(flow.getUpdatedAt().toString(),
                                html.contains(f2.format(flow.getUpdatedAt())));
                    }
                    assertTrue(flow.getCommentOn(), html.contains(getExpectedString(flow.getCommentOn())));
                }
            }

            index++;
        }
    } finally {
        zis.close();
        bi.close();
    }
}

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String Unzip(String zipFileName, String dstDirectory) {
    String sRet = "";
    String fixedZipFileName = fixFileName(zipFileName);
    String fixedDstDirectory = fixFileName(dstDirectory);
    String dstFileName = "";
    int nNumExtracted = 0;
    boolean bRet = false;

    try {/* ww w  .  j  av a  2  s. c  o m*/
        final int BUFFER = 2048;
        BufferedOutputStream dest = null;
        ZipFile zipFile = new ZipFile(fixedZipFileName);
        int nNumEntries = zipFile.size();
        zipFile.close();

        FileInputStream fis = new FileInputStream(fixedZipFileName);
        CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
        ZipEntry entry;

        byte[] data = new byte[BUFFER];

        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            if (fixedDstDirectory.length() > 0)
                dstFileName = fixedDstDirectory + entry.getName();
            else
                dstFileName = entry.getName();

            String tmpDir = dstFileName.substring(0, dstFileName.lastIndexOf('/'));
            File tmpFile = new File(tmpDir);
            if (!tmpFile.exists()) {
                bRet = tmpFile.mkdirs();
            } else
                bRet = true;

            if (bRet) {
                // if we aren't just creating a directory
                if (dstFileName.lastIndexOf('/') != (dstFileName.length() - 1)) {
                    // write out the file
                    FileOutputStream fos = new FileOutputStream(dstFileName);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                    dest = null;
                    fos.close();
                    fos = null;
                }
                nNumExtracted++;
            } else
                sRet += " - failed" + lineSep;
        }

        data = null;
        zis.close();
        System.out.println("Checksum:          " + checksum.getChecksum().getValue());
        sRet += "Checksum:          " + checksum.getChecksum().getValue();
        sRet += lineSep + nNumExtracted + " of " + nNumEntries + " successfully extracted";
    } catch (Exception e) {
        e.printStackTrace();
    }

    return (sRet);
}

From source file:org.etudes.jforum.view.admin.ImportExportAction.java

/**
 * write zip file to disk/*from ww w  .j av a2 s  .c  o  m*/
 * 
 * @param zis
 * @param name
 * @throws IOException
 */
private void unzip(ZipInputStream zis, String name) throws Exception {
    BufferedOutputStream dest = null;
    int count;
    byte data[] = new byte[BUFFER];
    try {
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(name);
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (dest != null)
                dest.close();
        } catch (IOException e1) {
        }
    }
}