473,763 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Zip files in other directories

Trying to figure out how to use the following program to zip up files
in another directory other than current "." Using current works fine,
I substitute it with a path "c:\\stuff" I get the following run-time
error:

Adding: file1.txt

java.io.FileNot FoundException: file1.txt (The system cannot find the
file specified)

at java.io.FileInp utStream.open(N ative Method)

at java.io.FileInp utStream.<init> (FileInputStrea m.java:103)

at java.io.FileInp utStream.<init> (FileInputStrea m.java:66)

at Zip.main(Zip.ja va:25)
It finds the first file but then fails. Here is the code:

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

public class Zip {
static int BUFFER = 4096;
static String dir = "c:\\stuff" ;
public static void main (String argv[]) {
try {
BufferedInputSt ream origin = null;
FileOutputStrea m dest = new
FileOutputStrea m("c:\\blah.zip ");
ZipOutputStream out = new ZipOutputStream (new
BufferedOutputS tream(dest));
//out.setMethod(Z ipOutputStream. STORED);

// get a list of files from current directory
File f = new File(dir);
String[] files = f.list();
byte data[] = new byte[BUFFER];

for (int i=0; i < files.length; i++) {

System.out.prin tln("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream (files[i]);
origin = new
BufferedInputSt ream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntr y(entry);
int count;
while((count = fi.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTra ce();
}
}
}

I am a Windows (XP) platform. Thanks for the help.
Jul 17 '05 #1
1 3133
or**********@ya hoo.com (Alex) wrote in message news:<11******* *************** ****@posting.go ogle.com>...
Trying to figure out how to use the following program to zip up files
in another directory other than current "." Using current works fine,
I substitute it with a path "c:\\stuff" I get the following run-time
error:


[snipped...]

IIRC the File.list() method returns an array of file and directory
names only, not including their path. When you were using them
to add to your zip file, the unqualified names would have therefore
defaulted to the current working directory.

To fix the problem, either pre-pend the path to each name ...or...
better still, use the File.listFiles( ) method to return an array
of File objects, instead of an array of String's. You can then
get the full name of the file from that object.

-FISH- ><>
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2991
by: IS | last post by:
Hi, I am trying to list all the files in a directory and add them to a collection. As well, I am trying to add all the files to this collection that might be in subdirectories (if exist). Any hints to how I do this? With Dir$ and such, I can find only single files and directories. I guess I should be using API functions I am not aware of. Thanks for your help,
44
4060
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are identical with which. Write a program that prints out which file are redundant copies. Here's the spec. -------------------------- The program is to be used on the command line. Its arguments are one or
5
1937
by: LarryM | last post by:
Hi, This is maybe not a pure ASP question, but has some relation: Please help me throw some light on this: Which directories and files are visible and readable for a (hacking) user at a Website: a) directories in the default website (obviously: YES) b) .asp-files in the default Website c) .asp-files in any diretory outside the default Website
10
9924
by: TokiDoki | last post by:
Hello there, I have been programming python for a little while, now. But as I am beginning to do more complex stuff, I am running into small organization problems. It is possible that what I want to obtain is not possible, but I would like the advice of more experienced python programmers. I am writing a relatively complex program in python that has now around 40 files.
8
2397
by: RML | last post by:
hey guys, i am looking at this piece of code that lists numbers of files in a directory. i want to convert it so it lists the files in th directory that end with .doc. i cant seem to get it to output correctly, i have included the original code, my modified code, and the output from my modifed code.. why does it list the files as "System.IO.FileInfo"??? thanks
4
7575
by: Jerry | last post by:
I'm having just a bit of trouble wrapping my brain around the task of working with folders that are above the site's root folder. I let users upload photos (.jpg/.gif files) which can subsequently be viewed on the site's pages. My hosting provider is requiring that any files my Web app writes get written to a folder that is above the app's root folder (for security purposes). When writing the files I understand how to use MapPath to...
4
4233
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not the 2 of them together. This is what I tried: Sub Page_Load(.....) Dim dInfo As DirectoryInfo dInfo = New DirectoryInfo(Server.MapPath(MyDir))
7
2299
by: elgiei | last post by:
Good morning at all, i have to implement a server,that every n-seconds (eg. 10sec) sends to other clients,which files and directory has been deleted or modified. i build a n-tree, for each files on harddisk there's a node into n- tree, this solution is not good for large hard disk.. and i can't use inotify (it's forbidden), and only c solutions are accepted without third party software or external calls.
30
2076
by: Jrdman | last post by:
hi there can any one write a C code that search for files in a directory without using the windows apis(FindFirstFile() and FindNextFile()) or even has a small idea on how to do that. thanks.
4
2449
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am sorting filetypes into subdirectories with same name. so far i have been able to create the subdirectories, but i am not able to copy the files into those directories. my code is : use strict; use warnings; use File::Glob;
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.