简体   繁体   English

如何从两个.doc文件创建一个.zip文件?

[英]How to create a .zip file from two .doc file?

I want to write a unit test to test creating .zip file from two .doc files. 我想编写一个单元测试来测试从两个.doc文件创建.zip文件。 BU I take an error: Error creating zip file: java.io.FileNotFoundException: D:\\file1.txt (The system cannot find the file specified) BU我出错:创建zip文件时出错:java.io.FileNotFoundException:D:\\ file1.txt(系统找不到指定的文件)

My code is here: 我的代码在这里:

@Test
public void testIsZipped() {

    String actualValue1 = "D:/file1.txt";
    String actualValue2 = "D:/file2.txt";

    String zipFile = "D:/file.zip";

    String[] srcFiles = { actualValue1, actualValue2 };

    try {

        // create byte buffer

        byte[] buffer = new byte[1024];

        FileOutputStream fos = new FileOutputStream(zipFile);
        zos = new ZipOutputStream(fos);

        for (int i = 0; i < srcFiles.length; i++) {

            File srcFile = new File(srcFiles[i]);

            FileInputStream fis = new FileInputStream(srcFile);

            // begin writing a new ZIP entry, positions the stream to the
            // start of the entry data

            zos.putNextEntry(new ZipEntry(srcFile.getName()));

            int length;

            while ((length = fis.read(buffer)) > 0) {

                zos.write(buffer, 0, length);
            }

            zos.closeEntry();

            // close the InputStream

            fis.close();
        }

        // close the ZipOutputStream

        zos.close();

    }

    catch (IOException ioe) {

        System.out.println("Error creating zip file: " + ioe);
    }

    String result = zos.toString();

    assertEquals("D:/file.zip", result);
}

Can I get name of zip file from zos to test, How to understand to pass the test? 我可以从zos获取zip文件的名称进行测试,如何理解通过测试? Can anybody help me to solve this error? 有人可以帮助我解决此错误吗? Thank you. 谢谢。

First, are your files created in a previous test method? 首先,您的文件是否使用先前的测试方法创建的? If yes consider that junit tests do not execute in the order you defined your test methods, have a look at this: 如果是,则认为junit测试未按照您定义测试方法的顺序执行,请查看以下内容:
How to run test methods in specific order in JUnit4? 如何在JUnit4中按特定顺序运行测试方法?

Second, you could add a debugging line: 其次,您可以添加一条调试行:

File srcFile = new File(srcFiles[i]);
System.out.append(srcFile+ ": " + srcFile.exists() + " " + srcFile.canRead());

After you solve the exception you will run into this problem, the test will fail: 解决异常后,您将遇到此问题,测试将失败:

String result = zos.toString();

assertEquals("D:/file.zip", result);

zos.toString() will return something like: "java.util.zip.ZipOutputStream@1ae369b7" which will not be equal to "D:/file.zip". zos.toString()将返回类似“ java.util.zip.ZipOutputStream@1ae369b7”的内容,该内容将不等于“ D:/file.zip”。

String zipFile = "D:/file.zip";
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
System.out.println(zos.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM