0

I try to put some file fileNamePath in zip archive (arguments are D:\text.txt D:\archive.zip):

  public static void main(String[] args) throws IOException {
    if (args.length==0) return;

    String fileNamePath = args[0];
    String zipPath = args[1];

    FileOutputStream outputStream = new FileOutputStream(zipPath);
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    zipOutputStream.putNextEntry(new ZipEntry(fileNamePath));

    File file = new File(fileNamePath);
    Files.copy(file.toPath(),zipOutputStream);

    zipOutputStream.closeEntry();
    zipOutputStream.close();

}

Archive is created but i don't see any file in it. Why?

SergeiK
  • 345
  • 1
  • 4
  • 19
  • 2
    Possible duplicate of [Adding files to ZIP file](http://stackoverflow.com/questions/10103861/adding-files-to-zip-file) – António Ribeiro Apr 23 '16 at 16:55
  • You don't see any file in it ? When I ran above code with same arguments It created a zip file with folder D: inside it and the text.txt was placed. – Vaibhav Jain Apr 23 '16 at 17:11
  • hmm, this kinda funny. when i extracted the archive file apeared. but it wasn't seen when i double-click on zip. i'm using win7 – SergeiK Apr 23 '16 at 17:33

1 Answers1

-1

That code is working perfectly:

zip.java

import java.io.*;
import java.nio.file.*;
import java.util.zip.*;

public class zip
{
public static void main(String[] args) throws IOException {
    if (args.length==0) return;

    String fileNamePath = args[0];
    String zipPath = args[1];

    FileOutputStream outputStream = new FileOutputStream(zipPath);
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    zipOutputStream.putNextEntry(new ZipEntry(fileNamePath));

    File file = new File(fileNamePath);
    Files.copy(file.toPath(),zipOutputStream);

    zipOutputStream.closeEntry();
    zipOutputStream.close();

}
}

I have compiled it under Debian 9 Stretch, OpenJDK 8

I have then created a sample txt file:

hello.txt

Hello World

I then compiled it:

javac zip.java

And finally run it:

java zip hello.txt hello.zip

I extract the .zip and open up hello.txt, returning Hello World

May it be that you have no permissions to read/write D:\?

Jesus Gonzalez
  • 411
  • 6
  • 17