-2

I have a question related to Java and Zip-files. Is there a way to make this into a .zip - file? I have a webpage where the text pops up (if you have the right log-in criterias), and how do you make that into a .zip format? (Because there is correct file-structur there)

The text is here:

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)
Mr.Turtle
  • 2,950
  • 6
  • 28
  • 46
  • Are you asking if there is a way to create zip files in Java? – Artur Nov 19 '13 at 19:11
  • You would zip it, basically... what code have you tried, and what happened? The fact that it's a text file is irrelevant. – Jon Skeet Nov 19 '13 at 19:11
  • Do u want to add client.txt to client.zip or just rename client.txt to client.zip – constantlearner Nov 19 '13 at 19:14
  • Hi! Thanks for commenting! Uhm... there is no .txt file exactly. There is just the text showing up on a web-page, and (it's a schools task) I'm supposed to get it back into a .zip – Mr.Turtle Nov 19 '13 at 19:17
  • See my answer below. Instead of using a text file as an argument, modify it to take a List of strings. For each string in the list write a line to the zip file. So you would replace the loop using in.readLine() with a for each loop of the List of strings. Get it? – Dodd10x Nov 19 '13 at 19:25
  • @OleReidarHolm - No problem. Please accept my answer if it helps. I can show you a modification as I described if you are having trouble, but just show me where you get stuck. – Dodd10x Nov 19 '13 at 19:39

1 Answers1

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;
    }
Dodd10x
  • 3,344
  • 1
  • 18
  • 27