Thread: Hey, I have a cachedownloading client, how do I make it download from the red bar?

Results 1 to 2 of 2
  1. #1 Hey, I have a cachedownloading client, how do I make it download from the red bar? 
    Donator
    ShotDown's Avatar
    Join Date
    Jan 2013
    Posts
    535
    Thanks given
    44
    Thanks received
    47
    Rep Power
    26
    Here's the cachedownloader file, it's a 614 client.
    Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    
    public class CacheDownloader extends JPanel {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -4838881615456366747L;
    	public String cacheLink = "http://dl.dropbox.com/u/44338767/Client_Cache.zip";
    	public String saveDirectory = cachedir.findcachedir();
    	public String fileToExtract = getSaveDirectory() + getFileName();
    	public JProgressBar progressBar;
    	public JFrame aJFrame;
    	public int currentVersion = 1;
    
    	public String getCacheLink() {
    		return cacheLink;
    	}
    
    	public String getSaveDirectory() {
    		return saveDirectory;
    	}
    
    	public int getCacheVersion() {
    		return currentVersion;
    	}
    
    	public CacheDownloader() {
    		progressBar = new JProgressBar(0, 100);
    		add(progressBar);
    		progressBar.setValue(0);
    		progressBar.setStringPainted(true);
    	}
    
    	public void updateProgressBar(int value) {
    		progressBar.setValue(value);
    	}
    
    	public CacheDownloader downloadCache() {
    		try {
    			File saveLocation = new File(getSaveDirectory());
    			File versionLocation = new File(getSaveDirectory() + "CacheVersion" + getCacheVersion() + ".dat");
    			if (!saveLocation.exists()) {
    				downloadFile(getCacheLink(), getFileName());
    				unZip();
    				BufferedWriter versionFile = new BufferedWriter(new FileWriter(getSaveDirectory() + "CacheVersion" + getCacheVersion() + ".dat"));
    				versionFile.close();
    				deleteDownloadedZipFile(getFileName());
    			} else {
    				if (!versionLocation.exists()) {
    					downloadFile(getCacheLink(), getFileName());
    					unZip();
    					BufferedWriter versionFile = new BufferedWriter(new FileWriter(getSaveDirectory() + "CacheVersion" + getCacheVersion() + ".dat"));
    					versionFile.close();
    					deleteDownloadedZipFile(getFileName());
    				} else
    					return null;
    			}
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		return null;
    	}
    
    	public void downloadFile(String address, String fileName) {
    		OutputStream fileDestination = null;
    		URLConnection fileToDownload;
    		InputStream readFileToDownload = null;
    		CacheDownloader download = new CacheDownloader();
    		JFrame frame = new JFrame("Downloading Cache");
    		frame.setResizable(false);
    		frame.setContentPane(download);
    		frame.setSize(250, 60);
    		frame.setVisible(true);
    		frame.setLocationRelativeTo(null);
    		try {
    			URL url = new URL(address);
    			fileDestination = new BufferedOutputStream(new FileOutputStream(getSaveDirectory() + fileName));
    			fileToDownload = url.openConnection();
    			readFileToDownload = fileToDownload.getInputStream();
    			byte[] data = new byte[1024];
    			int numRead;
    			long numWritten = 0;
    			int length = fileToDownload.getContentLength();
    			while ((numRead = readFileToDownload.read(data)) != -1) {
    				fileDestination.write(data, 0, numRead);
    				numWritten += numRead;
    				int percentage = (int)(((double)numWritten / (double)length) * 100D);
    				download.updateProgressBar(percentage);
    			}
    			frame.setVisible(false);
    			JOptionPane.showMessageDialog(this.aJFrame, "The Cache has finished downloading!");
    		} catch (Exception exception) {
    			exception.printStackTrace();
    		} finally {
    			try {
    				if (readFileToDownload != null)
    					readFileToDownload.close();
    				if (fileDestination != null)
    					fileDestination.close();
    			} catch (IOException ioe) {
    			}
    		}
    	}
    
    	public String getFileName() {
    		int lastSlashIndex = getCacheLink().lastIndexOf('/');
    		if (lastSlashIndex >= 0 && lastSlashIndex < getCacheLink().length() -1)
    			return getCacheLink().substring(lastSlashIndex + 1);
    		return "";
    	}
    
    	public void deleteDownloadedZipFile(String fileName) {
    		File f = new File(getSaveDirectory() + fileName);
    		if (!f.exists())
    			throw new IllegalArgumentException(
    					"Delete: no such file or directory: " + fileName);
    		if (!f.canWrite())
    			throw new IllegalArgumentException("Delete: write protected: "
    					+ fileName);
    		if (f.isDirectory()) {
    			String[] files = f.list();
    			if (files.length > 0)
    				throw new IllegalArgumentException(
    						"Delete: directory not empty: " + fileName);
    		}
    		boolean success = f.delete();
    		if (!success)
    			throw new IllegalArgumentException("Delete: deletion failed");
    	}
    
    	public void unZip() {
    		try {
    			InputStream in = new BufferedInputStream(new FileInputStream(fileToExtract));
    			ZipInputStream zin = new ZipInputStream(in);
    			ZipEntry e;
    			while ((e = zin.getNextEntry()) != null) {
    				if (e.isDirectory()) {
    					(new File(getSaveDirectory() + e.getName())).mkdir();
    				} else {
    					if (e.getName().equals(fileToExtract)) {
    						unzip(zin, fileToExtract);
    						break;
    					}
    					unzip(zin, getSaveDirectory() + e.getName());
    				}
    			}
    			zin.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public void unzip(ZipInputStream zin, String s) throws IOException {
    		FileOutputStream out = new FileOutputStream(s);
    		byte[] b = new byte[1024];
    		int len = 0;
    		while ((len = zin.read(b)) != -1) {
    			out.write(b, 0, len);
    		}
    		out.close();
    	}
    }
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    May 2012
    Posts
    187
    Thanks given
    115
    Thanks received
    27
    Rep Power
    12
    I lol'd because you post a class file and expect us to just throw you the answer. No one is going to. You need to find the loading bar and rename/refactor that code to make it easier to use/manipulate.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 6
    Last Post: 04-14-2013, 02:47 PM
  2. How do I make it so one client can connect
    By smokestudley in forum Help
    Replies: 0
    Last Post: 12-22-2010, 02:24 PM
  3. Replies: 6
    Last Post: 08-30-2009, 12:55 AM
  4. Replies: 5
    Last Post: 02-18-2008, 05:55 AM
  5. Replies: 6
    Last Post: 02-06-2008, 05:09 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •