Ok, so I wanted to open a pdf file that I put it in my jar, so I needed to copy the file from the jar to my disk, and I did so by the following code:
InputStream is = Jar.class.getResourceAsStream("images/lol.pdf");
OutputStream os = new FileOutputStream("753951741.pdf");
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
os.close();
is.close();
my question is:how do I control where the file is created?
When I execute the program It's created under C:/Users/Buba
Thanks in advance :)
You can do like this:
File file = new File("c:/753951741.pdf");
OutputStream os = new FileOutputStream(file);
In this case the file will be created in C:/
For more information about File in Java:
http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File(java.lang.String)
Related
I have tried this code for other files like .txt and excel files and they all are working fine. Same code if I use for pdf files, it will not show any error but when the file is downloaded, the zip folder will be corrupted. Is there any separate reading and writing process for pdf files?
//create byte buffer
byte[] buffer = new byte[1024];
ZipOutputStream zout = new
ZipOutputStream(response.getOutputStream());
Files[] files = new File(location).listFiles();
for(File pdfFile: files)
{
FileInputStream fin = new FileInputStream(pdfFile);
zout.putNextEntry(new ZipEntry(pdfFile);
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
zout.close();
This issue is resolved now. In the previous loop where I was creating the pdf files, the output stream was not closed and hence it was causing issue.
The below code is used to zip normal text file. When I extract using WinRaR its showing the content properly, but when I open with Windows Explorer its empty, no file listed. I am using Windows 7 Enterprise (64 bit) operating system. Any idea why its not listing in Windows explorer? Thanks in advance.
File file = new File("F:\\sample.txt");
byte[] buf = new byte[1024];
String outFilename = "F:\\zipped_sample.zip";
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.toString()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
out.flush();
}
out.closeEntry();
out.close();
in.close();
} catch (Exception e) {
// log exception here
}
ZipEntry constructor takes name but you are providing it a path by doing file.toString(); Try:
New ZipEntry(file.getName());
This will pass the file name.
i had the same problem zip file were not extracted and was shown as empty the problem was in the folder names in zip file. If folder name consist of > or < symbols i saw the failure . SO in the code new ZipEntry(file.toString()) i try to clean up any folder names with those particular symbols
I am trying to copy a file (Base.jar) to the same directory as the running jar file
I keep getting a corrupted jar file, that still holds the correct class structure when opened with winrar. What am I doing wrong? (I have also tried without the ZipInputStream, but that was no help) the byte[] is 20480 because that is size of it on the disk.
my code:
private static void getBaseFile() throws IOException
{
InputStream input = Resource.class.getResourceAsStream("Base.jar");
ZipInputStream zis = new ZipInputStream(input);
byte[] b = new byte[20480];
try {
zis.read(b);
} catch (IOException e) {
}
File dest = new File("Base.jar");
FileOutputStream fos = new FileOutputStream(dest);
fos.write(b);
fos.close();
input.close();
}
InputStream input = Resource.class.getResourceAsStream("Base.jar");
File fileOut = new File("your lib path");
OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();
and handle exceptions
No need to use ZipInputStream, unless you want to unzip the contents into memory and read.
Just use BufferedInputStream(InputStream) or BufferedReader(InputStreamReader(InputStream)).
did some more googling found this: (Convert InputStream to byte array in Java) worked for me
InputStream is = ...
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
(it looks very simular to the src for IOUtils.copy())
ZipInputStream is for reading files in the ZIP file format by entry. You need to copy the whole file (resource) that is you need to simply copy all bytes from InputStream no matter what format is. The best way to do it in Java 7 is this:
Files.copy(inputStream, targetPath, optionalCopyOptions);
see API for details
I have a method which zips up 5 files. It produces a zip file without error, but I cannot open it to examine the contents. I tried emailing it and gmail said it cannot send corrupt files. Trying to open with WinRAR in Windows results in an error stating:
The archive is either in unknown format or damaged
This is the method:
private void zipTestFiles() throws FileNotFoundException, IOException
{
File[] filenames = fileDir.listFiles(fileNameFilter(Constants.PAGE_MON_FILENAME_FILTER));
byte[] buf = new byte[1024];
String outFilename = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + Constants.PAGEMONITOR_ZIP;
DeflaterOutputStream out = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(outFilename)));
for (int i=0; i<filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
}
out.close();
}
You should use ZipOutputStream instead of DeflaterOutputStream. And do not forget to create entries. Read javadoc of ZipOutputStream before writing the implementation.
Try with ZipOutputStream which already exists in Java. DeflaterOutputStream only uses DEFLATE method to compress but doesn't put ZIP headers automatically.
I am having an issue putting a folder in a zip file I am trying to create. While the path is valid, when I run the code it gives me a File Not Found Exception.
Here is my code
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
byte[] buf = new byte[1024];
File file = new File("workspace");
System.out.println(file.isDirectory());
System.out.println(file.getAbsolutePath());
FileInputStream in = new FileInputStream(file.getAbsolutePath());
out.putNextEntry(new ZipEntry(file.getAbsolutePath()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
You're trying to read bytes from a directory; it doesn't work like that. The exception says as much, too.
You need to add the directory, then add each file within the directory. If you use the file path you don't need to add the directory explicitly.
I'd be very wary of using the absolute path as the zip entry; better to use a relative path so you can unzip it anywhere and not risk overwriting something you want.