Wrong path when zip a file
When I try to Zip a file(backup.zip) everything is good except one thing.
When I use WinZip to unzip the file, the path for the file contains all subdirectories eg. c:\mycatalog\backup\db\data.data
I would like the path to be like: backup\db\data.data
The code:
pathName is "c:\mycatalog"
fileName is "backup"
public class Zip {
private static ZipOutputStream zos;
* Creates a Zip archive. If the name of the file passed in is a
* directory, the directory's contents will be made into a Zip file.
public static void makeZip(File pathName, String fileName)
throws IOException, FileNotFoundException
File file = new File(pathName,fileName);
zos = new ZipOutputStream(new FileOutputStream(pathName.toString()+"\\"+fileName+".lum"));
//Call recursion.
recurseFiles(file);
//We are done adding entries to the zip archive,
//so close the Zip output stream.
zos.close();
* Recurses down a directory and its subdirectories to look for
* files to add to the Zip. If the current file being looked at
* is not a directory, the method adds it to the Zip file.
private static void recurseFiles(File file)
throws IOException, FileNotFoundException
if (file.isDirectory()) {
//Create an array with all of the files and subdirectories
//of the current directory.
String[] fileNames = file.list();
if (fileNames != null) {
//Recursively add each array entry to make sure that we get
//subdirectories as well as normal files in the directory.
for (int i=0; i<fileNames.length; i++) {
recurseFiles(new File(file, fileNames));
//Otherwise, a file so add it as an entry to the Zip file.
else {
byte[] buf = new byte[1024];
int len;
//Create a new Zip entry with the file's name.
ZipEntry zipEntry = new ZipEntry(file.toString());
//Create a buffered input stream out of the file
//we're trying to add into the Zip archive.
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
zos.putNextEntry(zipEntry);
//Read bytes from the file and write into the Zip archive.
while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
//Close the input stream.
in.close();
//Close this entry in the Zip stream.
zos.closeEntry();
Sorry, I want the path to be like: "\db\data.data"
Similar Messages
-
Java.lang.IndexOutOfBoundsException when zipping larger files
i get the error:
java.lang.IndexOutOfBoundsException
at java.util.zip.ZipOutputStream.write(ZipOutputStream.java:261)
at compress.zipEntry(compress.java:126)
at compress.Create_Zip_File(compress.java:84)
at Test.backup(Test.java:101)
at Test.<init>(Test.java:37)
at Test.main(Test.java:149)
when zipping a file which is 349MB in size I think this might be because it trying to store the file in the memory and the file is too large.What can I do to not store it in memory or if im wrong can someone point me in the right direction.
Thanks.
FileOutputStream zipFile = null;
ZipOutputStream zipOut = null;
private void listContents( File Zip_File, File dir, ZipOutputStream out )
throws Exception
// Assume that dir is a directory. List
// its contents, including the contents of
// subdirectories at all levels.
System.out.println("Directory \"" + dir.getName() + "\"");
String[] files ; // The names of the files in the directory.
files = dir.list();
for (int i = 0; i < files.length; i++) {
File f; // One of the files in the directory.
f = new File(dir, files);
if ( f.isDirectory() )
// Call listContents() recursively to
// list the contents of the directory, f.
listContents(Zip_File, f, out);
else
// For a regular file, just print the name, files[i].
zipEntry(Zip_File, f, out);
} // end listContents()
public void Create_Zip_File(File Zip_File,File[] To_Be_Zipped_Files,
boolean Skip_Dirs)
try
// Open archive file
FileOutputStream stream=new FileOutputStream(Zip_File);
ZipOutputStream out=new ZipOutputStream(stream);
for (int i=0;i<To_Be_Zipped_Files.length;i++)
//if (To_Be_Zipped_Files[i]==null
// || !To_Be_Zipped_Files[i].exists()
// || (Skip_Dirs ))
// continue;
System.out.println("Adding "+To_Be_Zipped_Files[i].getName());
zipEntry(Zip_File, To_Be_Zipped_Files[i], out);
out.close();
stream.close();
System.out.println("Finished zipping : "
+ Zip_File.getAbsolutePath());
catch (Exception e)
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
return;
private void zipEntry(File Zip_File, File file, ZipOutputStream out)
throws Exception
if (file.isDirectory())
listContents(Zip_File, file, out);
return;
int BUFFER_SIZE=10240000; // 10 M
byte buffer[]=new byte[BUFFER_SIZE];
// Add archive entry
ZipEntry zipAdd=new ZipEntry(file.getAbsolutePath());
//zipAdd.setTime(file.lastModified());
out.putNextEntry(zipAdd);
// Read input & write to output
FileInputStream in=
new FileInputStream(file.getAbsolutePath());
int len;
int nRead=in.available();
while((count = in.read(buffer, 0, BUFFER_SIZE)) != -1)
out.write(buffer, 0, nRead);
// System.out.println("loop ");
in.close();
out.closeEntry();of course!!Thanks problem solved:DI don't see how - it is very very wrong!
private void zipEntry(File Zip_File, File file, ZipOutputStream out)
throws Exception
if (file.isDirectory())
listContents(Zip_File, file, out);
return;
int BUFFER_SIZE=4094;
byte buffer[]=new byte[BUFFER_SIZE];
// Add archive entry
ZipEntry zipAdd=new ZipEntry(file.getAbsolutePath());
//zipAdd.setTime(file.lastModified());
out.putNextEntry(zipAdd);
// Read input & write to output
InputStream in= new BufferedInputStream( new FileInputStream(file.getAbsolutePath()), BUFFER_SIZE);
for(int count = 0; (count = in.read(buffer)) != -1;)
out.write(buffer, 0, count);
in.close();
out.closeEntry();
}Message was edited by:
sabre150 -
File not found Exception when zipping a file
Hello I seem to have run into a problem when trying to zip a directory.
I get the error:
java.io.FileNotFoundException: C:\Users\dojo\Documents\Programming\compress (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at ZipUtility.zip(ZipUtility.java:31)
at ZipUtility.main(ZipUtility.java:21)
when i run the code
import java.io.File;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.*;
public class ZipUtility {
public ZipOutputStream cpZipOutputStream = null;
public String strSource = "";
public String strTarget = "";
public static long size = 0;
public static int numOfFiles = 0;
public static void main(String args[]) {
ZipUtility udZipUtility = new ZipUtility();
udZipUtility.strSource = "C:\\Users\\dojo\\Documents\\Programming\\compress\\restoreDump"; //args[0];
udZipUtility.strTarget = "C:\\Users\\dojo\\Documents\\Programming\\compress"; //args[1];
udZipUtility.zip();
public void zip(){
try {
File cpFile = new File (strSource);
if (!cpFile.isFile() && !cpFile.isDirectory() ) {
System.out.println("\nSource file/directory Not Found!");
return;
FileOutputStream fos = new FileOutputStream(strTarget);
cpZipOutputStream = new ZipOutputStream(fos);
cpZipOutputStream.setLevel(9);
zipFiles( cpFile);
cpZipOutputStream.finish();
cpZipOutputStream.close();
System.out.println("\n Finished creating zip file " + strTarget + " from source " + strSource);
System.out.println("\n Total of " + numOfFiles +" files are Zipped " );
System.out.println("\n Total of " + size + " bytes are Zipped ");
} catch (Exception e){
e.printStackTrace();
public void zipFiles(File cpFile) {
int byteCount;
final int DATA_BLOCK_SIZE = 2048;
FileInputStream cpFileInputStream;
if (cpFile.isDirectory()) {
if(cpFile.getName().equalsIgnoreCase(".metadata")){ //if directory name is .metadata, skip it.
return;
File [] fList = cpFile.listFiles() ;
for (int i=0; i< fList.length; i++){
zipFiles(fList) ;
else {
try {
if(cpFile.getAbsolutePath().equalsIgnoreCase(strTarget)){
return;
System.out.println("Zipping "+cpFile);
size += cpFile.length();
//String strAbsPath = cpFile.getAbsolutePath();
numOfFiles++;
String strAbsPath = cpFile.getPath();
String strZipEntryName = strAbsPath.substring(strSource.length()+1, strAbsPath.length());
//byte[] b = new byte[ (int)(cpFile.length()) ];
cpFileInputStream = new FileInputStream (cpFile) ;
ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
cpZipOutputStream.putNextEntry(cpZipEntry );
byte[] b = new byte[DATA_BLOCK_SIZE];
while ( (byteCount = cpFileInputStream.read(b, 0, DATA_BLOCK_SIZE)) != -1)
cpZipOutputStream.write(b, 0, byteCount);
//cpZipOutputStream.write(b, 0, (int)cpFile.length());
cpZipOutputStream.closeEntry() ;
} catch (Exception e) {
e.printStackTrace();
I really have no idea why its saying access denied if anyone can help that would be great.son_goku
Read the stack trace from the bottom up. It's telling you that the error, or exception arose
at ZipUtility.zip(ZipUtility.java:21)
which is udZipUtility.zip();This line called the zip() method which threw an exception
at ZipUtility.main(ZipUtility.java:31)
which is FileOutputStream fos = new FileOutputStream(strTarget);What is the value of strTarget?"C:\\Users\\dojo\\Documents\\Programming\\compress"This is a folder, isn't it?
Read the javadoc for public FileOutputStream(String name)
throws FileNotFoundException
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Regards, Darryl -
See file path when opening a file is CS
I have wondered this for some time. When I open/place a file into CS, it will by default open to the most recent files and folders accessed. Sometimes I have more than one folder by the same name (one on server, one on my desktop for example). I often would like to double check the path where these files are located. But the path does not show up when opening a file from the application. Show path bar is only visible when you access the files straight from the finder. Is there any way to see the path from "open or place"? I often have to re-establish the path to ensure I have the right one.
Update your video card driver from the GPU maker's website.
-
Remove path when attach library file
What is the impact if I don remove the path when I attach the library file ? Pls advise. Thanks.
Ideally you should include the path where you have placed the library in FORMS60_PATH and remove path while attaching it.This ensures that forms automatically picks up your library file.
Hence the path is not hardcoded and you don't have to change the forms code.If you don't remove path, you may face problem when you run your code in a different machine. -
Keeping paths when zipping files
Hi,
I have a problem :
I have to zip a directory that contains sub-directories, and mail this zip file.
I've managed to get all the files into one zip, but when I unzip it (winzip), all the paths are gone ...
any ideas that can help me to keep them?if you use winzip, you can just highlight all the directories/files you want in it and right click (add to zip), and all the paths will be kept.
-
"File Dialog" Express VI returns the wrong path when the user selects the desktop
In labview 8.0.1 with Windows
XP, the File Dialog Express VI seems to return something other than the path
selected by the user when configured to browse for folders (or files and
folders) and the user selects the desktop.
I can't determine exactly when it works and when it fails, but it always seems
to fail if the user selects the desktop from the places bar (the shortcuts on
the left side of the dialog) or from the drop-down menu at the top of the
dialog and then clicks the "Current Folder" button to dismiss the
dialog. In this case it always returns whatever path is wired to the
"Start Path" terminal.
If the user navigates to the desktop with the "Up One Level" button
or by selecting the actual desktop directory under Documents and Settings, the
correct path is returned.
If the user selects a file before clicking the desktop icon, then clicks
"Current Folder" the correct path is returned.
If the user clicks the desktop icon, clicks open, then clicks "Current
Folder" the correct path is returned.
It's possible to work around this bug in most cases by setting the start path
to the desktop.
Has anyone seen this behavior before?
Can anybody verify this behavior by running the attached VI?
In the past, when I've reported issues like this I've received responses from
NI engineers that basically said "Thanks, but we already knew
that". Is there anywhere that users can search the list of known
bugs and avoid wasting time tracking down issues that are already understood by
NI?
Thanks,
Adam Brewster
Attachments:
File Dialog Test.vi 39 KBI'm on 8.2 and I can't get it to fail no matter how I select the desktop. I haven't tried it in 8.0, but I imagine that if this really is a bug, they did know about it and fixed it with 8.2. If this causes a real problem for you, I would suggest upgrading if you can.
-
__MACOSX foldres when zipping a file
Hello,
i often need to make a zip file on my mac and put it on the internet for others to download. when i then open the zipfile i see a __MACOSX folder.
obviously this is for the resource fork and junk like that. this is very confusing for Windows users. So how do i suppress this ?
tyvmYes, those directories contain resource fork information and other metadata. I don't know of a way to suppress this if you use the built-in "compress" function in the Finder. However, you can easily create a zip file from the command line using the "zip" command (or tar/gz or bzip2 commands, for that matter), which won't contain this extra data.
Additionally, you could probably also write an Automator workflow that could zip up a folder or a bunch of selected objects without the added stuff. -
How to not display the file path when converting a file to pdf from the context menu
When I right click an html file and choose convert to pdf, Acrobat puts the original file's path and creation date at the bottom of the pdf, how do I stop it doing this?
excellent, thanks. For future readers:
open acrobat
click create
click create from web page
click settings...
under PDF Settings uncheck "Place headers and footers on new page"
click ok -
CVAW package : wrong filename when saving pdf file
Hi experts,
In my bsp, I have a link to the standard bsp CVAW_VIEW_DOCFILE in order to display an original of a DIR.
An example of a complete URL to open an original :
https://daplmdv.sylvania.com:10443/sap/bc/bsp/sap/CVAW_VIEW_DOCFILE/ViewDocFile.htm?pa_document_key=ZPI000000000000000000185252800EN&pa_file_id=49A5C8136B110187E1008000C61CAA2D&filename=d:\seal\data\convserv\49A5B257145901AAE1008000C61CAA2D\in\test.pdf
In this case, the pdf has been displayed but when I use the button "Save a copy" in acrobat reader, the windows proposed "ViewDocFile.pdf" instead of "test.pdf".
In fact, I have always this name when I open the document first and try to save it after.
With .doc files, I have another window which ask me if I want to open or save the document (I think I have this window because the doc is not automaticaly opened in internet explorer). So, when I save it directly, the name is correct but when I open it and then save it, MS word propose me the name "ViewDocFile.doc".
Do you have an idea how I can have the original name and not ViewDocFile.ext ?
Thanks for your supportHi Regis,
You have put this thread as answered.
I have the same problem, can you tell me how you fixed this problem?
thanks!
KR,
Micha -
How to get complete path when uploading a file in Mozzila
Hi
I was trying to upload a file using html code
<input type="file" name="cdrfile">
then I wrote
<% = request.getParameter("cdrfile")%>
trying to get the path and the file name. But Mozilla doesn't return me the complete path only the file name. Does anyone know how to get the complete path in Mozilla?
Cheers
MDeliberate double post:
http://forum.java.sun.com/thread.jspa?threadID=5147786&messageID=9551206#9551206 -
I am trying to change the path when downloading a file using the download_db procedure (webutil_file_transfer.db_to_client).
Thanks,
TerryIs the procedure DB_TO_CLIENT built in oracle?
I also want to download video files from Oracle to client and save to d:\video, How can I do?
Appreciate for your any words.
If you know, Plz email to:
[email protected] -
Path in Classpath for file SapMetamodelWebdynproContent.zip not found.
How to get rid of these warnings?
[Warning]: Path in Classpath for file SapMetamodelWebdynproContent.zip not found.
Warning [Warning]: Version for file SapMetamodelWebdynproContent.zip not found.
Warning [Warning]: Versions of 'SapDictionaryTypeServices.jar' have different prefix.
Warning [Warning]: Versions of 'SapDictionaryTypesRuntime.jar' have different prefix.
thanks in advance.Hi,
This is a common problem when you import projects to NWDS. Do like this.
1.Change to Navigator tab in NWDS and delete gen_wdp folder.
2.Switch to Web Dynpro Explorer. Select the Project -> Right click-> Select properties -> in the wizard select Java Build Path -> Select Libraries tab -> You will find jar files with warnings
3.Remove the jar and add the jar file with same name to the project one by one.
4.For each jar file repeat step 3.
5.Save the project
Or
removed the read-only property from all folders / files of the project and it solved my problem.
Hope this helps!!
Thanks & Regards
Vijay K -
File Dialog can return incorrect path when selecting Windows special folders
I found an interesting bug when writing an application today. Basically the File Dialog express VI can return the wrong path if selecting special folders in some circumstances. This bug seems especially tough to track down, but I think I managed to figure it out. Steps to reproduce using LabVIEW 8.6.1 on Windows XP:
Use LabVIEW's File > Open... command to open any VI that is not located directly on your desktop. You MUST use the File > Open... command as this will set the working directory for the dialog box.
Close the VI (all we cared about was ensuring the working directory was set).
Create a new VI.
Place the File Dialog express VI onto the diagram (Programming > File I/O > Advanced File Functions > File Dialog).
Configure the express VI to select a folder and an existing item, leave the selection enabled to limit to a single item.
Create an indicator to show the returned path from the dialog.
Run the VI. Navigate to either your Desktop via the large button on the left, or the pull down menu up top.
Click the "Current Folder" button.
Observe how the VI returns whatever the original location of the dialog box was, not the desktop directory.
I have not confirmed this bug on other operating systems but following the steps above reproduces it on my system.This is a very unusual problem you are having. I tried it on another PC in the office and it did not show the problem you are experiencing. Are you able to replicate this every time, or only occasionally? Are you able to see this on other computers? Do you see this same problem using other software on the computer? Do you have the most recent updates from Microsoft? Perhaps the answer to these questions can help shed some light on the problem at hand.
David_L | Certified LabVIEW Architect
LabVIEW Tools Network | LabVIEW Tools Network Developer Center -
Revision: 21260
Revision: 21260
Author: [email protected]
Date: 2011-05-16 07:46:54 -0700 (Mon, 16 May 2011)
Log Message:
Update the qa-frameworks.zip to remove all comments from the base xml when merging config files.
Modified Paths:
blazeds/trunk/qa/resources/frameworks/qa-frameworks.zipTry options=('!makeflags') in PKGBUILD.
Maybe you are looking for
-
Is there a way to print a Html-Report direktly in LabView?
I created a Html-Report wich includes a jpg-Picture. Actually there is no way, in the Report Generation, to Print a Html file. I Would be glad if you have a solution (sub-vi?) to Print a Html file directly in Labview. Thanks PH!L
-
I cannot use my Acrobat XI downloaded with my one year subscription. It tells me my trial has expired and i need to license it. but I have no serial number.
-
MBP 2.16 Core Duo - ProRes
Hi Everybody, I have the original Macbook Pro with 2.16 Intel core Duo processor not the core 2. My questions is will I be able to edit 1080i prores - not hq on this machine or do I need to upgrade? I plan to capture on the new AJA Ki Pro. If so, how
-
It's been 4 days now my App Store show 2 message it means that I have to update some applications but every time I open my App Store and press update it only shows blank page but if I press genius, top charts, featured, search I can browse them
-
Hey, I am currently migrating my cluster. I removed the server pool master according to the metalink note by doing a failover (stopped the agent on the server pool master) Deleted the old master (node2) from the server pool. Executed the cleanup scri