I m getting this Error, when I upload to file with zipfile.
Error: Error creating zip file: java.io.FileNotFoundException: C:\fupload\qhT39xmU- (The system cannot find the file specified)
This is My Upload Method :
public String uploadToFts(UploadedFile filem, String fileType){
String fileFtsUrl = null;
String uploadUrl = fileDAO.findByUniqueProperty("name", "geturl").getPropValue();
String usr= fileDAO.findByUniqueProperty("name", "usr").getPropValue();
String pwd= fileDAO.findByUniqueProperty("name", "pwd").getPropValue();
String nameId= String.valueOf(passGen.create(1, 1, 8, 8, 0)) + "-";
try{
ClientConfig cc = new DefaultClientConfig();
Client client = Client.create(cc);
client.addFilter(new HTTPBasicAuthFilter(usr, pwd));
try {
FormDataMultiPart form = new FormDataMultiPart();
File file = new File("C:/fupload/"+nameId+"");
File thumbnail = new File("C:/fupload/"+nameId+"-tmb.jpg");
zipFile("C:/fupload/"+nameId+".zip", file, thumbnail);
File zipFile = new File("C:/fupload/"+nameId+".zip");
String urlParams = "nameId=" + nameId+ "&" +
"fileType="+fileType+"&" +
"fileName=" + zipFile.getName() + "&" +
"zipped=true";
form.bodyPart(new FileDataBodyPart("file", zipFile, MediaType.MULTIPART_FORM_DATA_TYPE));
WebResource resource = client.resource(uploadUrl + urlParams);
ClientResponse response = resource.type(MediaType.APPLICATION_OCTET_STREAM).put(ClientResponse.class, zipFile);
String respStr = response.getEntity(String.class);
if(respStr.contains("\"status\":0")){
System.out.println(respStr);
JSONObject obj = new JSONObject(respStr);
int jsonStatus = obj.getInt("status");
int jsonTxnId = obj.getInt("txnid");
String url = obj.getString("url");
fileFtsUrl = url;
} else {
addMessageToView(FacesMessage.SEVERITY_ERROR, "", "Can't uploading FTS"+respStr);
}
} catch (Exception e) {
e.printStackTrace();
}
return fileFtsUrl;
} catch (Exception e){
e.printStackTrace();
return fileFtsUrl;
}
}
ZipFile Method
public static void zipFile(String zipFile, File... srcFiles) {
try {
// create byte buffer
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (int i = 0; i < srcFiles.length; i++) {
File srcFile = 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);
}
}
I think you just need to create the file in zipFile(..), as it won't exist:
File file = new File(zipFile);
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file, false);
Or maybe better to create it before and pass it to zipFile(file, ...), so you won't have to create it again just after the call. So pass a File instead of a String as with the other arguments.
Check with
System.out.println("C:/fupload/"+nameId+"")
System.out.println("C:/fupload/"+nameId+"-tmb.jpg")
Is exist in your system?
Make sure your path is must point file in your system
Related
I need to develop an API which can open connection to an URL which returns a ZIP file. This URL works perfectly fine when accessed from browser or Postman but when I try to access it from Java Code (tried HttpClient/RestTemplate etc) it returns an HTML file. I want to get the zip file and want to store it at particular directory.
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpGet request = new HttpGet(url);
logger.info("Request to Asset Store: URL " + request.getURI());
HttpResponse response = client.execute(request);
if (response != null) {
for (Header header : response.getAllHeaders()) {
System.out.println(header.getName() + " - " + header.getValue());
}
BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
final ZipInputStream is = new ZipInputStream(bis);
try {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(),
entry.getSize(), new Date(entry.getTime()));
extractEntry(entry, is);
}
System.out.println("OUT");
} finally {
is.close();
}
}
private static void extractEntry(final ZipEntry entry, InputStream is) throws IOException {
String exractedFile = "D://" + entry.getName();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(exractedFile);
final byte[] buf = new byte[2048];
int read = 0;
int length;
while ((length = is.read(buf, 0, buf.length)) >= 0) {
fos.write(buf, 0, length);
}
} catch (IOException ioex) {
fos.close();
I've created a pretty simple Java GUI to browse/load a zip file on Windows platform to begin unzipping and then do some file checking.
Everything works fine except that I have to close the GUI window in order to delete the zip file that has been opened in the GUI.In my finally block of the unzipping method, I've tried adding the following:
public static String unZip(String path)
{
int count = -1;
String savepath = "";
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
savepath = path.substring(0, path.lastIndexOf("\\")) + File.separator; //File saving directory
new File(savepath).mkdir(); //create the saving directory
ZipFile zipFile = null;
String topLevelDirName="";
try
{
zipFile = new ZipFile(path,Charset.forName("gbk")); //Encoding
Enumeration<?> entries = zipFile.entries();
int levelCount=0;
while(entries.hasMoreElements())
{
byte buf[] = new byte[buffer];
ZipEntry entry = (ZipEntry)entries.nextElement();
String filename = entry.getName();
boolean ismkdir = false;
if(filename.lastIndexOf("/") != -1){ //To check if there is a directory
ismkdir = true;
}
filename = savepath + filename;
if(entry.isDirectory()){ //If it is a directory
levelCount++;
file = new File(filename);
file.mkdirs();
if(levelCount==1)
topLevelDirName = filename;
continue;
}
file = new File(filename);
if(!file.exists()){
if(ismkdir){
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs();
}
}
file.createNewFile(); //Create the file
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);
while((count = is.read(buf)) > -1)
{
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
try{
if(bos != null){
bos.close();
}
if(fos != null) {
fos.close();
}
if(is != null){
is.close();
}
if(zipFile != null){
zipFile.close();
}
}catch(Exception e) {
e.printStackTrace();
}
return topLevelDirName;
}
}
However, I am still not able to delete the zip unless explicitly close the GUI.
Wonder if there is anything to do with the Windows file handle?Thanks in advance.
Java 8 introduced the try-with-resources Statement to make this kind of situation simpler and cleaner.
One of the issues you have is, if any one of the attempts to close the many resources you have open fails, then none of the others will be closed
public static String unZip(String path) throws IOException {
int count = -1;
File sourceFile = new File(path);
String name = sourceFile.getName();
name = name.substring(0, name.lastIndexOf(".zip"));
File sourcePath = new File(sourceFile.getParent(), name);
System.out.println("SavePath = " + sourcePath);
if (!sourcePath.exists() && !sourcePath.mkdirs()) {
throw new IOException("Could not create directory " + sourcePath);
}
String topLevelDirName = "";
try (ZipFile zipFile = new ZipFile(sourceFile)) {
Enumeration<?> entries = zipFile.entries();
int levelCount = 0;
byte buf[] = new byte[1024];
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String filename = entry.getName();
File file = new File(sourcePath, filename);
if (entry.isDirectory()) { //If it is a directory
levelCount++;
System.out.println("Make directory " + file);
if (!file.exists() && !file.mkdirs()) {
throw new IOException("Could not create directory " + filename);
}
} else {
System.out.println("Extract to " + file);
try (InputStream is = zipFile.getInputStream(entry);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
while ((count = is.read(buf)) > -1) {
bos.write(buf, 0, count);
}
}
}
}
}
return topLevelDirName;
}
I've update the code slightly to try and make it a little cleaner and simpler and to take advantage of the available APIs
I need to download a file from the following link in Java
[http://www.nseindia.com/content/historical/EQUITIES/2017/OCT/cm30OCT2017bhav.csv.zip][1]
I have the code written in C#, can some one suggest Java equivalent code
WebClient webClient = new WebClient();
String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
String agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1";
webClient.Headers.Add(HttpRequestHeader.Accept, accept);
webClient.Headers.Add(HttpRequestHeader.UserAgent, agent);
webClient.UseDefaultCredentials = true;
webClient.DownloadFile(source, target);
I myself found a solution
source = "http://www.bseindia.com/download/Bhavcopy/Derivative/bhavcopy07-11-17.zip";
target = "d:\Market Feeds\EQD BSE Bhavcopy\"
public static void downloadFileHttp(String source, String destination) throws Exception {
try{
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipaddress, port));
URL oracle = new URL(source);
URLConnection yc = oracle.openConnection(proxy);
InputStream in = yc.getInputStream();
FileOutputStream out = new FileOutputStream(destination + "\\bhavcopy.zip");
copySource2Dest(in, out, 1024);
out.close();
extractFolder(destination + "\\bhavcopy.zip", destination);
//Path path = FileSystems.getDefault().getPath(destination, "bhavcopy.zip");
//boolean succ = Files.deleteIfExists(path);
System.out.println("Download is successfull");
}
catch(Exception e){
System.out.println("Error in downloading : " + e);
}
}
public static void copySource2Dest(InputStream input, OutputStream output, int bufferSize)
throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}
public static void extractFolder(String zipFile,String extractFolder)
{
try
{
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = extractFolder;
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
ZipEntry entry;
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
zip.close();
}
catch (Exception e){
System.out.println("ERROR: "+e.getMessage());
}
}
This question already has answers here:
directories in a zip file when using java.util.zip.ZipOutputStream
(6 answers)
Closed 9 years ago.
My task requires me to save a file directory into a zip folder. My only problem is I need to keep the sub-folders as folders from the main Directory. The file system will look something like
C\\Friends
C:\\Friends\\Person1\\Information.txt
C:\\Friends\\Person2\\Information.txt
C:\\Friends\\Person3\\Information.txt
.
.
.
Right now I am able to write just the txt files inside of my zip folder, but in my zip folder I need to keep that folder structure. I know the way my code is right now will tell me the file I'm trying to write is closed(No access). My Functions thus far:
private String userDirectroy = "" //This is set earlier in the program
public void exportFriends(String pathToFile)
{
String source = pathToFile + ".zip";
try
{
String sourceDir = userDirectory;
String zipFile = source;
try
{
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File fileSource = new File(sourceDir);
addDirectory(zout, fileSource);
zout.close();
System.out.println("Zip file has been created!");
}
catch(Exception e)
{
}
}
catch(Exception e)
{
System.err.println("First Function: " + e);
}
}
private static void addDirectory(ZipOutputStream zout, File fileSource) {
File[] files = fileSource.listFiles();
System.out.println("Adding directory " + fileSource.getName());
for(int i=0; i < files.length; i++)
{
if(files[i].isDirectory())
{
try
{
byte[] buffer = new byte[1024];
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
}
catch(Exception e)
{
System.err.println(e);
}
addDirectory(zout, files[i]);
continue;
}
try
{
System.out.println("Adding file " + files[i].getName());
//create byte buffer
byte[] buffer = new byte[1024];
//create object of FileInputStream
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
//close the InputStream
fin.close();
}
catch(IOException ioe)
{
System.out.println("IOException :" + ioe);
}
}
}
Any help would be much appreciated. Thank You
For each folder, you need to add a empty ZipEntry of the path.
For each file, you need to supply both the path and file name. This will require you to know the part of the path to strip off, this would be everything after the start directory
Expanded concept
So, from your example, if the start directory is C:\Friends, then the entry for C:\Friends\Person1\Information.txt should look like Person1\Information.txt
public void exportFriends(String pathToFile) {
String source = pathToFile + ".zip";
try {
String sourceDir = "C:/Friends";
String zipFile = source;
try {
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File fileSource = new File(sourceDir);
addDirectory(zout, sourceDir, fileSource);
zout.close();
System.out.println("Zip file has been created!");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getRelativePath(String sourceDir, File file) {
// Trim off the start of source dir path...
String path = file.getPath().substring(sourceDir.length());
if (path.startsWith(File.pathSeparator)) {
path = path.substring(1);
}
return path;
}
private static void addDirectory(ZipOutputStream zout, String sourceDir, File fileSource) throws IOException {
if (fileSource.isDirectory()) {
// Add the directory to the zip entry...
String path = getRelativePath(sourceDir, fileSource);
if (path.trim().length() > 0) {
ZipEntry ze = new ZipEntry(getRelativePath(sourceDir, fileSource));
zout.putNextEntry(ze);
zout.closeEntry();
}
File[] files = fileSource.listFiles();
System.out.println("Adding directory " + fileSource.getName());
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDirectory(zout, sourceDir, files[i]);
} else {
System.out.println("Adding file " + files[i].getName());
//create byte buffer
byte[] buffer = new byte[1024];
//create object of FileInputStream
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(getRelativePath(sourceDir, files[i])));
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
//close the InputStream
fin.close();
}
}
}
}
On a server hosted on google app engine, I am trying to download files from another server, zip (using util.zip) them and upload the zip file for later download.
I have files in a folder (html and png files). The download and zip and upload is successful. I can download the desired zip, however I can not open the png files, even though I can the original ones. It says the program doesn't support the file format. Interestingly the there are no problems with the html files in the zip.
Does anybody know what can be the problem here?
Thank you in advance.
--THE CODE--
public boolean generateZip(){
byte[] application = new byte[1500000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(baos);
//This will get the desired file names and locations from the other server
ArrayList<String> others = getFileNames(this);
for(String s: others){
URL url = new URL("http://otherserver.com/" + s);
BufferedReader reader = null;
try{
//I need only the file names not the full directory name
int toSub = s.lastIndexOf("/");
String entryString = s.substring(toSub+1);
out.putNextEntry(new ZipEntry(entryString));
reader = new BufferedReader(new InputStreamReader(url.openStream()));
byte[] buffer = new byte[3000000];
int bindex = 0;
int b = reader.read();
while(b != -1){
buffer[bindex] = (byte) b;
bindex++;
b = reader.read();
}
out.write(buffer,0,bindex);
out.closeEntry();
reader.close();
System.out.println(entryString + " packaged...");
}catch(Exception e){
e.printStackTrace();
}
}
}
out.close();
} catch (IOException e) {
System.out.println("There was an error generating ZIP.");
e.printStackTrace();
}
return uploadZip(baos.toByteArray());
}
Finally I found the solution. For anyone who's interested:
for(String s: others){
URL url = new URL("http://otherserver.com" + s);
System.out.println("Reading " + s);
try{
int toSub = s.lastIndexOf("/");
String entryString = s.substring(toSub+1);
out.putNextEntry(new ZipEntry(entryString));
BufferedInputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
BufferedOutputStream out2 = new BufferedOutputStream(baos2);
int i;
while ((i = in.read()) != -1) {
out2.write(i);
}
out2.flush();
byte[] data = baos2.toByteArray();
// closing all the shits
out2.close();
in.close();
out.write(data,0,data.length);
out.closeEntry();
System.out.println(entryString + " packaged...");
}catch(Exception e){
e.printStackTrace();
}
}