压缩Java的文本。有办法吗?网页上的文字为.zip

时间:2013-11-19 19:07:28

标签: java text zip converter

我有一个与Java和Zip文件有关的问题。 有没有办法把它变成.zip文件? 我有一个弹出文本的网页(如果你有正确的登录标准),你如何将其变成.zip格式? (因为那里有正确的文件结构)

案文在这里:

https://www.dropbox.com/s/il24ih0fu4h7iqn/client.txt
It does **NOT** show up as a .txt, thats just me adding it for you (easier readability)

1 个答案:

答案 0 :(得分:2)

http://docs.oracle.com/javase/7/docs/api/java/util/zip/package-summary.html

 public static boolean zipFile(final File fileToZip, final File zippedFile) {
            boolean successStatus = false;
            BufferedReader in = null;
            ZipOutputStream out = null;

        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(
                    fileToZip), "UTF-8"));
            out = new ZipOutputStream(new FileOutputStream(zippedFile));
            out.putNextEntry(new ZipEntry(fileToZip.getName()));
            String line;
            final byte[] newLine = System.getProperty("line.separator")
                    .getBytes("UTF-8");

            while ((line = in.readLine()) != null) {
                final byte[] buffer = line.getBytes("UTF-8");
                out.write(buffer, 0, buffer.length);
                out.write(newLine, 0, newLine.length);
            }

            out.closeEntry();
            out.finish();
            successStatus = true;
        } catch (final IOException ex) {
            successStatus = false;
        } finally {
            try {
                if (in != null) {
                    in.close();
                    in = null;
                }
            } catch (final IOException ex) {

            }
            try {
                if (out != null) {
                    out.close();
                    out = null;
                }
            } catch (final IOException ex) {

            }
        }
        return successStatus;
    }