Path in ZIP Files
I have a program that compresses files into a ZIP File.
How do you get the data extractable in something like WinZip and how do you get Ride of the Path in WinZip?
Well what I'm trying to do is use the class files in java.util.zip.* to make a program that compresses files into a ZIP Archive. I figured out the question you didn't understand. What I'm trying to do right now is get the data in the archive extractable. When I open my archive that I created with my program, WinZip says that the enties are there. When I try to extract them WinZip crashes. I used a code that is something like this:
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outzip));
int q = 0;
for (q = 0; q < infile.length; q++) {
FileInputStream in = new FileInputStream(infile[q]);
size = in.available();
byte b[] = new byte[size];
in.read(b);
File f = new File(infile[q]);
ZipEntry ze = new ZipEntry(f.getPath());
zos.setLevel(9);
ze.setTime(f.lastModified());
zos.putNextEntry(ze);
zos.write(b);
zos.closeEntry();
in.close();
zos.flush();
zos.close();
Infile is an array of file names in the form of strings. Then the program loops through them strings turning each into a file so that it is written without a path name (which wa part of my last problem) into the zip file.
Similar Messages
-
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.
-
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"
-
TutWD_Popup_Init.zip File path to download...
HI all,
pls provide me the path to download TutWD_Popup_Init.zip file.
Regards
RaviHi,
use the below code. YES and NO are event handlers here.
IWDEventHandlerInfo ev1=wdControllerAPI.getViewInfo().getViewController().findInEventHandlers("YES");
IWDEventHandlerInfo ev2=wdControllerAPI.getViewInfo().getViewController().findInEventHandlers("NO");
IWDConfirmationDialog dlg=wdComponentAPI.getWindowManager().createConfirmationWindow("No Customer Exists. Do you like to create new Customer?",ev1,"Yes");
dlg.addChoice(ev2,"No");
dlg.show(); -
Error while extracting zip file
I am using the following code to extract zip files from jar file
source = "zip/test.zip"
destination = "c:\\"
public void unzip(String source,String destination){
try{
ZipInputStream in = new ZipInputStream( this.getClass().getResourceAsStream(source));
FileOutputStream fos = null;
ZipEntry ze = null;
File ff = null;
while ( (ze=in.getNextEntry() ) != null ){
System.out.println( "name=" + ze.getName() );
ff = new File( destination + ze.getName() );
if ( ze.isDirectory() ){
System.out.println("making "+ze.getName()+" dir ("+ ff.mkdirs() +")");
else{
fos = new FileOutputStream( ff );
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
fos.write(buf, 0, len);
fos.close();
System.out.println("wrote file "+ff.getPath()+" success=" + ff.exists() );
in.close();
}catch(Exception e){
e.printStackTrace();
} But i found error in extracting zip file
java.io.FileNotFoundException: C:\eclipse\.eclipseproduct (The system cannot find the path specified)
I need some quick response on it.Nothing to do with the resource path....thats fine. Use the code below.....most likely this will fix it.......
Added a check for directories that start with a ".". The File utility probably thinks its a file, also added a try....check it does make the "." directories.........dont forget the dukes.
ZipInputStream zis = null;
FileOutputStream fos = null;
try
zis = new ZipInputStream( new FileInputStream( "C:\\your_path\\your_zipfile_or_jar.zip" ) );
ZipEntry ze = null;
File ff = null;
while ( ( ze = zis.getNextEntry() ) != null )
System.out.println( "name=" + ze.getName() );
ff = new File( "C:\\your_path\\" + ze.getName() );
if ( ze.isDirectory() || ze.getName().startsWith("."))
try{ System.out.println( "making " + ze.getName() + " dir (" + ff.mkdirs() + ")" ); }catch(Exception e2){}
else
fos = new FileOutputStream( ff );
byte[] buf = new byte[1024];
int len;
while ( ( len = zis.read( buf ) ) > 0 )
fos.write( buf, 0, len );
fos.close();
System.out.println( "wrote file " + ff.getPath() + " success=" + ff.exists() );
zis.close();
catch( Exception ex )
ex.printStackTrace();
finally
try { zis.close(); }catch(Exception ex){}
try { fos.close(); }catch(Exception ex){}
} -
Creating a zip file from the contents of a directory
hi, I am having a problem as the title suggests with a zip fil creation...
using the basic example zip.java i wished to edit it so it doesnt zip a file fro the current directory but rather a directory i inputted.
It is able to read the first file then throws out the following error with the code below it:
java.io.FileNotFoundException: test.jpg (The system cannot find the file specified)
import java.io.*;
import java.util.zip.*;
public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/test.zip");
CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
ZipOutputStream out = new
ZipOutputStream(new BufferedOutputStream(checksum));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/");
f.listFiles();
String files[] = f.list();
for (int i=0; i<files.length; i++)
System.out.println("Adding: "+files);
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1)
out.write(data, 0, count);
origin.close();
out.close();
catch(Exception e)
e.printStackTrace();
After investigation i am lead to believe this is because the method returns an array of file and directory names only but not their path and the unqualified names would have therefore be defaulted to the current working directory. Something i understand if this is the case.
So instead i used the File.listFiles() method to return an array
of File objects, instead of an array of Strings as shown in the snippet of the changed code below (the changed code highlighted)...but arrived at another error on the second section of highlighted code meaning i cant compile. I cant understand why this is so!
The error is: "cannot find symbol, Symbol: Contructor ZipEntry (Java.IO.file), location: class.java.util.zip.ZipEntry"
File f = new File("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/");
**************File g[] = f.listFiles();***************
**************String files[] = f.list();**************
for (int i=0; i<g.length; i++)
System.out.println("Adding: "+g);
FileInputStream fi = new FileInputStream(g[i]);
origin = new BufferedInputStream(fi, BUFFER);
**************ZipEntry entry = new ZipEntry(g[i]);************
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1)
out.write(data, 0, count);
origin.close();
out.close();
Any help and thoughts most appreciated. Thank u in advanceI'll admit i took 1 look at that reply an thought "thats stupid that wont work"...
then a second look an though "actually, i should work i cant believe i didnt think of that"
Anyways i tried it and it did work
How are the duke dollars awarded, cos u should have them ordinary_guy
cheers! -
Adding zip file to classpath or -Xbootclasspath on J9
Hi,
I'm trying to add a zip file containing javax classes (jscience-vm14.zip) to the classpath when running my program. I haven't had any problems adding jar files for instance, but I can't seem to have this zip file included so that it can be recognized by the jvm.
I've noticed in the installation document of the J9 that if you want to add javax classes it has to be put on the -Xbootclasspath. So I've tried including the zip file using -Xbootclasspath/p:path/to/zipfile/zipfile.zip and I've tried -Xbootclasspath/a:path/to/zipfile/zipfile.zip. I've also tried just including it in the classpath with no luck either. I keep getting a NoClassDefFoundError: javax.units.Unit which is part of the jscience package
Any ideas how to get the zip file included?
Kind regards
SimonHi,
Thank you for your answer!
I'm using SWT and here's the code:
lstBread.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
System.out.println("Widget selected!");
It's working wtihout error on win32 platform. But there is no call on Windows Mobile 5.0 platform. It doesn't run and no exception is throwed on J9 console.
However, when i use the same way for event handling in SWT Button, there is no problem both on WM5 and win32 platform. It's executed as i want.
The code for SWT Button:
btnSView.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
System.out.println("Widget selected!");
I couldn't find the reason of that. Why doesn't the list#addListener run on WMobile?
Best Regards,
Ceyhun Hallac -
To upload the ZIP file and get the filenames available in ZIP file in ABAP
Hi Experts,
For my requirement, file from legacy comes as ZIP file with number of files in that.
Please provide one code sample to upload the ZIP file from local workstation and get the filenames available in ZIP file to check few filename validation checks for the available files in report program.
Thanks in Advance,
Regards,
Basani1. Copy the ZIP file into App server
2. Call function
call function 'RFC_REMOTE_PIPE'
destination 'SERVER_EXEC'
exporting
command = command " Unzip command gunzip /path & file
read = 'X'
tables
pipedata = std_lines
then you can read the files and can validate the file names
a® -
Zipped files created with Java won't unzip with Java
Hello there,
I have written a class for unzipping a zip file using the 'ZipFile' class. It works perfectly fine when I extract zip files that have been created with XP, Winzip, or Winrar.
I am now experimenting with creating zip files using the ZipOutputStream (http://forum.java.sun.com/thread.jspa?forumID=256&threadID=366550 by author smeee). The code works great for creating the zip file, but when I try and unzip it with the zipfile class mentioned above it throws an exception.
The error that the following code gives me when it tries to convert an element from the enumeration to a ZipEntry is this: java.io.FileNotFoundException: C:\testfiles\out\high\BAUMAN\00001.jpg (The system cannot find the path specified)
NOTE: The file is there by the way!!! :-)
See the code for extracting here:
try {
zippy = new ZipFile(fileName);
Enumeration all = zippy.entries();
while (all.hasMoreElements()) {//loop through all zip entries
getFile((ZipEntry)all.nextElement()); <<<=====FAILS HERE
} catch (IOException err) {
System.err.println(err.toString());
return;
}Now if I extract the zip file with winzip, then rezip it with winzip and run the above method again it works with no errors. Any thoughts. Any help would be greatly appreciated.
JaredHello All,
For anyone else who use the forum posting by smeee as a guide to create a zipper (http://forum.java.sun.com/thread.jspa?forumID=256&threadID=366550 by author smeee).
I was tracing through the code and found that there is a statement that adds 1 character (strSource.length()+1) to the source path. This was causing the following bug:
In windows it was placing objects like this \myfolder\myfile.txt
In unix it was placing objects like this yfolder\myfile.txt
Naturally a path like \myfolder... in the zip index was causing problems. I have added a case statement that tests the OS and then adds two chars if windows to compensate for the 'C:' and does nothing if Unix. The code now runs perfectly on either OS.
Thanks for your response guys!
Jared -
SQLJ in Oracle 10.1.0.2 missing translator.zip file
Hi Friends
I have installed Oracle 10.1.0.2 on my system and trying to use the SQLJ feature.
The Translator.zip file which contains the SQLJ library is missing after the installation.
The path where it is supposed to be present is as below
F:\oracle\product\10.1.0\Db_1\sqlj\lib
Does the Oracle 10.1.0.2 supports SQLJ feature and why is the translator.zip file missing.
Can you please let me know if this feature is currently supported for this specific oracle version.
Is there any problem in the installation or some other issue.
Please let me know if anybody has faced this issue previously and any help would be very helpful.
Thanks & Regards
Vikram KOracle Database 10.anything is in desupport mode. This is a really bad time to begin implementation and development with such an old version.
My recommendation would be to remove what you installed and install 11.2.0.1 which you can download from http://otn.oracle.com.
~ Posted from Montevideo Uruguay -
XML file with an attached MIME encoded ZIP file
Hi all,
I'm new to SAP WAS and MIME encoding/decoding, and I'm trying to generate an XML file with an attachment which is also MIME encoded.
1) I have dummy files (1.jpg, 2.jpg) and I'm trying to zip these files into one zip file (files.zip).
2) I'm trying to MIME encode/decode this zip file.
3) I'm trying to attach this MIME encoded zip file to existing XML file.
Which FMs could I use to accomplish this? Your help is very appreciated.
Thank you.
below is a file example that I'm trying to generate.
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="--XXXXboundary text"
Content-Transfer-Encoding: 7bit
This is a multi-part message in MIME format.
--XXXXboundary text
Content-Type: text/xml;
charset="utf-8"
Content-Transfer-Encoding: 8bit
<?xml version="1.0" encoding="utf-8"?>
<abc/>
<def/>
--XXXXboundary text
Content-Type: application/octet-stream; name="files.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
UEsDBBQAAAAIAI9EejAs5k34H84DAAYgBAAMAAAAMDAyMjQ5MTEucGRmnLsJWBNJ2zb6jmJIIIFE
BAMIJGEVBSKGRRAhEGQNoGwKYoiAEnYRUFGIkBAQFzYXRNHAAEGQZRy2ATFDUAHfGScSIUwQMMrM
ECGADptA0n/jzLtc//dd51znVAKdru6uqn7q6fu5764qQzJust create an applet (extend JApplet)... add a JTextArea to it....fill the text area with the text from an XML doc.
To get the text of the XML doc just do something like..
File xmlFile = new File("<path to xml file>");
FileInputStream fis = new FileInputStream(xmlFile);
byte[] bytes = new byte[(int) xmlFile.length()];
fis.read(bytes);
fis.close();
String xmlText = new String(xmlBytes);
textArea.setText(xmlText);
...try something like that (assuming..i understand what it is u want) -
Sending HR-File as email by the ABAP program as password protected ZIP file
Hi All,
My requiremet is to directly email the SAP-HR files to the users as the password protected ZIP file on UNIX.
Can anyone help me out how to implement this in my ABAP program.
Regards,
Saumikhi,
To populate data in different column you may use the below code.
DATA : filename TYPE string VALUE "Path
DATA :BEGIN OF wa_string,
data TYPE string,
END OF wa_string.
DATA : it_data LIKE STANDARD TABLE OF wa_string,
data TYPE string.
DATA: v_tab TYPE char1.
v_tab = cl_abap_char_utilities=>horizontal_tab.
CONCATENATE 'happy' 'new year' INTO wa_string-data SEPARATED BY v_tab.
APPEND wa_string TO it_data .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = filename
TABLES
data_tab = it_data.
IF sy-subrc <> 0.
ENDIF. -
How to read/unzip a specific file within a zip file
Hi,
I have a file within a zip file that contains a timestamp - I want to read this timestamp and then create a destination directory for the remaining zip files to be unzipped into. Since I know the name of the file with the timestamp in it I thought I could create a zipfile and use getEntry to get the entry but then other than getting the size and name of the file I can't do much more with it like read it unless I use a stream (zipinputstream) instead of a file (zipfile) - do I have this right?
Does this mean to get the content I would have to loop through possibly all the files using the stream until I come across the one I want - then get the timestamp and loop through them all again to write them to the destination directory? Or am I reading this wrong - seems a bit round about.
Any suggestions would be greatly appreciated.
Thanksthis works though - and you don't have to loop through all the files - just use the ZipFile:
ZipEntry ze = zipfile.getEntry("path/to/file");
BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
line = br.readLine;Thanks! -
How to place a binary zip files using sftp receiver adapter
Hi experts,
we got one scenario , where we need to pick the file (.zip) from local source path and place it in SFTP server path.
we have done all configuration setting and processed a zip to SFTP server.
but the client are saying the .zip file is not in a binary formate.
so could any please help me and provide solution on this issue.
regards
rajuhi Praveen,
i clicked binary mode option in sender channel, and at receiver side sftp adapter channel i not found any option.
could you please help me.
regards
raju -
How to upload a PDF file, zip it and download the zipped file?
Hi Experts,
I have a requirement to upload a PDF file, convert that to a ZIP file and download it. If anyone has worked on this requirement, can you please guide me on this? Thanks.
AviHere you go. Hope it helps.
REPORT zrich_0004.
DATA: lt_data TYPE TABLE OF x255.
DATA: ls_data LIKE LINE OF lt_data.
DATA: lv_zip_content TYPE xstring.
DATA: lv_size TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_path TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lt_filetab TYPE TABLE OF file_table.
DATA: ls_filetab LIKE LINE OF lt_filetab.
DATA: lv_rc TYPE sy-subrc.
DATA: lv_content TYPE xstring.
DATA: lo_zip TYPE REF TO cl_abap_zip.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_up TYPE string DEFAULT 'C:\upload.pdf' .
PARAMETERS: p_down TYPE string DEFAULT 'C:\download.zip' .
SELECTION-SCREEN END OF BLOCK b1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_up.
REFRESH lt_filetab. CLEAR ls_filetab.
cl_gui_frontend_services=>file_open_dialog(
CHANGING
file_table = lt_filetab
rc = lv_rc
EXCEPTIONS
OTHERS = 5 ).
READ TABLE lt_filetab INTO ls_filetab INDEX 1.
IF sy-subrc = 0.
p_up = ls_filetab-filename.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_down.
CLEAR: lv_filename, lv_path, lv_fullpath.
cl_gui_frontend_services=>file_save_dialog(
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
EXCEPTIONS
OTHERS = 4 ).
p_down = lv_fullpath.
START-OF-SELECTION.
CREATE OBJECT lo_zip.
* Read the data as a string
cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = p_up
filetype = 'BIN'
IMPORTING
filelength = lv_size
CHANGING
data_tab = lt_data
EXCEPTIONS
OTHERS = 19 ).
* convert binary to xstring
CLEAR lv_content .
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_size
IMPORTING
buffer = lv_content
TABLES
binary_tab = lt_data
EXCEPTIONS
failed = 1
OTHERS = 2.
* Get the file name of the uploaded file
DATA: lv_upfilename TYPE string.
DATA: lv_tmp TYPE char1024.
DATA: lv_tmp_file TYPE char1024.
lv_tmp = p_up.
CALL FUNCTION 'STRING_REVERSE'
EXPORTING
string = lv_tmp
lang = sy-langu
IMPORTING
rstring = lv_tmp.
SPLIT lv_tmp AT '\' INTO lv_tmp_file lv_tmp.
CALL FUNCTION 'STRING_REVERSE'
EXPORTING
string = lv_tmp_file
lang = sy-langu
IMPORTING
rstring = lv_tmp_file.
lv_upfilename = lv_tmp_file.
* add to zip file.
lo_zip->add( name = lv_upfilename content = lv_content ).
lv_zip_content = lo_zip->save( ).
* Conver the xstring content to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_zip_content
IMPORTING
output_length = lv_size
TABLES
binary_tab = lt_data.
* download
cl_gui_frontend_services=>gui_download(
EXPORTING
bin_filesize = lv_size
filename = p_down
filetype = 'BIN'
CHANGING
data_tab = lt_data
EXCEPTIONS
OTHERS = 24 ).
Regards,
Rich Heilman
Maybe you are looking for
-
Why do I not have calendars on iOS 6?
After updating my two iPhone 4s, I have lost all of my iCloud calenders. On my new iPhone 5 I also do not see my events. I have deleted my iCloud, and then readded it, but to no avail. My contacts, and email are working fine in iCloud. My son's 3
-
Command that Works in Terminal Not Running as Launch Agent
Trying to run the following command as a Launch Agent (OS X 10.7): /usr/bin/find /Users/username/Documents/Dropbox/Public -type f -mtime +2 -print0 | xargs -0 rm -r Created a plist file (reproduced below) and saved to ~/Library/Launch Agents, but the
-
EZedia Plug-ins for iMovie: Vol. 1 1.2.6
Hi out there! Anyone try using this plug in? .......eZedia Plug-ins for iMovie: Vol. 1 1.2.6 If so, how did you like it and did it work well with imovie? Was it user friendly? etc. etc. Thanks, Simonne
-
How do I alphabetize my table of contents?
I find an old answer, but It's not working for me. Do I have to change every song headin(heading 2) to a numbered list individually?
-
Lightroom 2.6 preference file location on Windows 7
I have just installed windows 7 on my laptop and want to copy my preference file from my desktop Vista machine over so I have the same setup. I located the preference file in vista under users/myname/appdata/roaming/adobe/lightroom as per the post on